Пример #1
0
        public static List<CatTable> Fill(DbJet db)
        {
            OleDbDataReader reader = db.Read("select * from 0cat order by catord asc");

            List<CatTable> tables = new List<CatTable>();
            Dictionary<string, object> fields = new Dictionary<string, object>();

            while (reader.Read())
            {
                // Fields will help us to overcome non seq order
                fields.Clear();
                for (int i = 0; i < reader.FieldCount; i++)
                {
                    fields.Add(reader.GetName(i).ToLower(), reader.GetValue(i));
                }

                CatTable table = new CatTable();
                table.id = long.Parse(fields["id"].ToString());
                table.name = fields["name"].ToString();
                table.catord = long.Parse(fields["catord"].ToString());
                table.lvl = long.Parse(fields["lvl"].ToString());

                tables.Add(table);
            }

            return tables;
        }
Пример #2
0
        private void UpdateBooks()
        {
            if (listFiles.Items.Contains("main.mdb"))
            {
                DbJet reader = new DbJet();
                int index = listFiles.Items.IndexOf("main.mdb");

                reader.Connect(_files[index]);

                OleDbDataReader read = reader.Read("select * from 0bok");

                _boks = BokTable.Fill(reader);
                _cats = CatTable.Fill(reader);

                listBooks.Items.Clear();
                int counter = 1;
                foreach (BokTable bok in _boks)
                {
                    listBooks.Items.Add(string.Format("{0}.{1}", counter++, bok.Bk));
                }

                lblBooks.Text = string.Format("{0} Books", _boks.Count);
            }
            else
            {
                MessageBox.Show("Ouch!, Can't find main.mdb");
            }
        }
Пример #3
0
        private void Convert(List<BokTable> tables)
        {
            if (tables.Count < 1)
            {
                MessageBox.Show("Please select at least 1 book to convert, or click Convert All", "Warning!", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            btnConvertSel.Enabled = false;
            btnConvertAll.Enabled = false;
            btnStop.Enabled = true;

            string targetPath = edtiShamelaTargetPath.Text;
            if (Directory.Exists(targetPath) == false)
            {
                Directory.CreateDirectory(targetPath);
            }

            Directory.SetCurrentDirectory(targetPath);

            _activeWorker = new BackgroundWorker();
            _activeWorker.WorkerReportsProgress = true;
            _activeWorker.WorkerSupportsCancellation = true;

            ProgressState state = new ProgressState();

            _activeWorker.DoWork += delegate(object sender, DoWorkEventArgs e)
            {

                RSSGenerator rssGen = new RSSGenerator();

                if (_activeWorker.CancellationPending)
                {
                    e.Cancel = true;
                }

                state.BookMaximum = tables.Count;

                foreach (BokTable table in tables)
                {
                    long bkid = table.BkId;
                    string bk = table.Bk;
                    long archive = table.Archive;

                    state.BookCounter++;
                    state.CurrentBook = bk;

                    DbSqlite sqlite = new DbSqlite();
                    string fname = string.Format("{0}.aza", bk);
                    fname = fname.Replace('=', '-');
                    fname = fname.Replace(':', '-');

                    string azaPrefixName = string.Format("{0}", bkid);
                    string azaName = string.Format("{0}.aza", bkid);

                    if (sqlite.Connect(Path.Combine(targetPath, azaName)) == false)
                    {
                        // TODO
                    }

                    string query = DbSqlite.CreateMainTableDefinition();
                    sqlite.Query(query);

                    sqlite.CreateMainTable(table);

                    if (archive == 0)   // Books are not stored in archive
                    {
                        string dbName = string.Format("{0}.mdb", bkid);

                        if (_mdbs.Contains(dbName))
                        {
                            int index = _mdbs.IndexOf(dbName);
                            string filename = _files[index];

                            // Connect to the book database
                            DbJet db = new DbJet();
                            db.Connect(filename);

                            // Get max records
                            int bookRowCount = db.QueryScalar("select count(*) from book");
                            int titleRowCount = db.QueryScalar("select count(*) from title");

                            // Query the book
                            OleDbDataReader reader = db.Read("select * from book");

                            // Create table query string
                            query = DbSqlite.CreateTableDefinition(string.Format("b{0}", bkid), DbJet.GetDataTypes(reader));

                            // Create book table
                            sqlite.Query(query);

                            int recno = 1;

                            state.RecordMaximum = bookRowCount + titleRowCount;
                            state.RecordState = "Book Contents";

                            // Now Convert all the records
                            while (reader.Read())
                            {
                                string fieldnames = DbJet.GetDataFieldNames(reader);
                                string fieldvalues = DbJet.GetDataFieldValues(reader);
                                query = DbSqlite.InsertTableDefinition(string.Format("b{0}", bkid), fieldnames, fieldvalues);

                                sqlite.Query(query);

                                state.RecordCounter = recno++;

                                _activeWorker.ReportProgress(recno, state);
                            }

                            reader.Close();

                            state.RecordState = "Table Of Contents";

                            reader = db.Read("select * from title");
                            query = DbSqlite.CreateTableDefinition(string.Format("t{0}", bkid), DbJet.GetDataTypes(reader));

                            sqlite.Query(query);

                            // Now Convert all the records
                            while (reader.Read())
                            {
                                string fieldnames = DbJet.GetDataFieldNames(reader);
                                string fieldvalues = DbJet.GetDataFieldValues(reader);
                                query = DbSqlite.InsertTableDefinition(string.Format("t{0}", bkid), fieldnames, fieldvalues);

                                sqlite.Query(query);

                                state.RecordCounter = recno++;

                                _activeWorker.ReportProgress(recno, state);
                            }

                            reader.Close();
                        }

                    }
                    else  // If books stored as an archive
                    {
                        string dbName = string.Format("{0}.mdb", archive);

                        if (_archivesFiles.Contains(dbName))//_archives.Contains(dbName))
                        {
                            int index = _archivesFiles.IndexOf(dbName);
                            string filename = _archives[index];

                            // Connect to the book database
                            DbJet db = new DbJet();
                            db.Connect(filename);

                            // Get max records
                            int bookRowCount = db.QueryScalar(string.Format("SELECT COUNT(*) FROM b{0}", bkid));
                            int titleRowCount = db.QueryScalar(string.Format("select count(*) from t{0}", bkid));

                            // Query the book
                            OleDbDataReader reader = db.Read(string.Format("select * from b{0}", bkid));

                            // Create table query string
                            query = DbSqlite.CreateTableDefinition(string.Format("b{0}", bkid), DbJet.GetDataTypes(reader));

                            // Create book table
                            sqlite.Query(query);

                            int recno = 1;

                            state.RecordMaximum = bookRowCount + titleRowCount;
                            state.RecordState = "Book Contents";

                            // Now Convert all the records
                            while (reader.Read())
                            {
                                string fieldnames = DbJet.GetDataFieldNames(reader);
                                string fieldvalues = DbJet.GetDataFieldValues(reader);
                                query = DbSqlite.InsertTableDefinition(string.Format("b{0}", bkid), fieldnames, fieldvalues);

                                sqlite.Query(query);

                                state.RecordCounter = recno++;

                                _activeWorker.ReportProgress(recno, state);
                            }

                            reader.Close();

                            state.RecordState = "Table Of Contents";

                            reader = db.Read(string.Format("select * from t{0}", bkid));
                            query = DbSqlite.CreateTableDefinition(string.Format("t{0}", bkid), DbJet.GetDataTypes(reader));

                            sqlite.Query(query);

                            // Now Convert all the records
                            while (reader.Read())
                            {
                                string fieldnames = DbJet.GetDataFieldNames(reader);
                                string fieldvalues = DbJet.GetDataFieldValues(reader);
                                query = DbSqlite.InsertTableDefinition(string.Format("t{0}", bkid), fieldnames, fieldvalues);

                                sqlite.Query(query);

                                state.RecordCounter = recno++;

                                _activeWorker.ReportProgress(recno, state);
                            }

                            reader.Close();
                        }
                    }

                    sqlite.Disconnect();

                    Manifest manifest = new Manifest();
                    manifest.Title = bk;
                    manifest.Author = table.AuthInf;
                    manifest.Category = table.Cat;
                    manifest.Link = azaName;

                    string plist = Plist.PlistDocument.CreateDocument(manifest);
                    string manifestName = Path.Combine(targetPath, "Manifest.plist");
                    StreamWriter writer = new StreamWriter(manifestName);
                    writer.Write(plist);
                    writer.Flush();
                    writer.Close();

                    RSSItem rssItem = rssGen.AddItem();
                    rssItem.Title = table.BkId.ToString();
                    rssItem.PubDate = DateTime.Now;
                    rssItem.Link = string.Format("{0}.azx", azaPrefixName);
                    rssItem.Author = table.AuthInf;
                    rssItem.Description = table.Bk;

                    using (ZipFile zip = new ZipFile())
                    {

                        string zipName = azaName;// Path.Combine(targetPath, azaName);

                        try
                        {
                            zip.AddFile(zipName);
                            zip.AddFile("Manifest.plist");
                            zip.Save(string.Format("{0}.azx", azaPrefixName));
                        }
                        finally
                        {
                            File.Delete(zipName);
                            File.Delete(manifestName);
                        }
                    }
                }

                rssGen.FeedXML(string.Format("Books-{0}.rss", DateTime.Now));
            };

            _activeWorker.ProgressChanged += delegate(object sender, ProgressChangedEventArgs e)
            {
                ProgressState ps = e.UserState as ProgressState;
                if (ps.BookCounter > ps.BookMaximum)
                {
                    ps.BookCounter = ps.BookMaximum;
                }

                pbBooks.Value = ps.BookCounter;
                pbBooks.Maximum = ps.BookMaximum;
                pbBooks.Minimum = ps.BookMinimum;
                lblBookStatus.Text = string.Format("Book {0} of {1} : {2}", ps.BookCounter, ps.BookMaximum, ps.CurrentBook);

                if (ps.RecordCounter > ps.RecordMaximum)
                {
                    ps.RecordCounter = ps.RecordMaximum;
                }
                pbRecords.Value = ps.RecordCounter;
                pbRecords.Maximum = ps.RecordMaximum;
                pbRecords.Minimum = ps.RecordMinimum;
                lblRecordStatus.Text = string.Format("Processing {0}: Record {1} of {2}", ps.RecordState, ps.RecordCounter, ps.RecordMaximum);
            };

            _activeWorker.RunWorkerCompleted += delegate(object sender, RunWorkerCompletedEventArgs e)
            {
                if (e.Cancelled)
                {
                    MessageBox.Show("Convert Canceled", "iShamela", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("Convert Completed", "iShamela", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                btnConvertSel.Enabled = true;
                btnConvertAll.Enabled = true;
                btnStop.Enabled = false;
                _activeWorker = null;
                pbBooks.Value = 0;
                pbRecords.Value = 0;
                lblBookStatus.Text = "Ready";
                lblRecordStatus.Text = "Ready";
            };

            _activeWorker.RunWorkerAsync();
        }