📄 cdmanager.cpp
字号:
return true;}//-----------------------------------------------------------------------------/// Logout from the DB; give an opportunity to save changes//-----------------------------------------------------------------------------void CDManager::logout () { TRACE8 ("CDManager::logout ()"); on_delete_event (NULL); for (unsigned int i (0); i < (sizeof (pages) / sizeof (*pages)); ++i) pages[i]->clear (); Words::destroy (); Storage::logout (); enableMenus (false); status.pop (); status.push (_("Disconnected!"));}//-----------------------------------------------------------------------------/// Edits dthe preferences//-----------------------------------------------------------------------------void CDManager::savePreferences () { TRACE9 ("CDManager::savePreferences ()"); if (opt.pINIFile) { TRACE5 ("CDManager::savePreferences () - " << opt.pINIFile); std::ofstream inifile (opt.pINIFile); if (inifile) { inifile << "[Database]\nUser=" << opt.getUser () << "\nPassword=" << opt.getPassword () << "\n\n"; YGP::INIFile::write (inifile, "Export", opt);#ifdef WITH_MOVIES inifile << "[Movies]\nLanguage=" << Movie::currLang << '\n';#endif } else { Glib::ustring msg (_("Can't create file `%1'!\n\nReason: %2.")); msg.replace (msg.find ("%1"), 2, opt.pINIFile); msg.replace (msg.find ("%2"), 2, strerror (errno)); Gtk::MessageDialog dlg (msg, Gtk::MESSAGE_ERROR); dlg.run (); } } // Storing the special/first names and the articles try { Storage::startTransaction (); Storage::deleteNames (); Words::forEachName (0, Words::cNames (), &Storage::storeWord); Storage::commitTransaction (); Storage::startTransaction (); Storage::deleteArticles (); Words::forEachArticle (0, Words::cArticles (), &Storage::storeArticle); Storage::commitTransaction (); } catch (std::exception& e) { Storage::abortTransaction (); Glib::ustring msg (_("Can't store special names!\n\nReason: %1.")); msg.replace (msg.find ("%1"), 2, e.what ()); Gtk::MessageDialog dlg (msg, Gtk::MESSAGE_ERROR); dlg.run (); }}#if (WITH_RECORDS == 1) || (WITH_MOVIES == 1)//-----------------------------------------------------------------------------/// Exports the stored information to HTML documents//-----------------------------------------------------------------------------void CDManager::export2HTML () { std::string dir (opt.getDirOutput ()); if (dir.size () && (dir[dir.size () - 1] != YGP::File::DIRSEPARATOR)) { dir += YGP::File::DIRSEPARATOR; opt.setDirOutput (dir); } // Load data for (unsigned int i (0); i < (WITH_RECORDS + WITH_MOVIES); ++i) if (!pages[i]->isLoaded ()) pages[i]->loadData (); // Get the key for the shared memory holding the special words std::ostringstream memKey; memKey << Words::getMemoryKey () << std::ends; YGP::Tokenize langs (LANGUAGES); std::string lang; const char* envLang (getenv ("LANGUAGE")); std::string oldLang; if (envLang) oldLang = envLang; std::string key (memKey.str ()); const char* args[] = { "CDWriter", "--outputDir", opt.getDirOutput ().c_str (),#if WITH_RECORDS == 1 "--recHeader", opt.getRHeader ().c_str (), "--recFooter", opt.getRFooter ().c_str (),#endif#if WITH_MOVIES == 1 "--movieHeader", opt.getMHeader ().c_str (), "--movieFooter", opt.getMFooter ().c_str (),#endif NULL, key.c_str (), NULL }; const unsigned int POS_LANG ((sizeof (args) / sizeof (*args)) - 3); Check2 (!args[POS_LANG]); // Export to every language supported Glib::ustring statMsg (_("Exporting (language %1) ...")); while ((lang = langs.getNextNode (' ')).size ()) { TRACE6 ("CDManager::export2HTML () - Lang: " << lang); Glib::ustring stat (statMsg); stat.replace (stat.find ("%1"), 2, Language::findInternational (lang)); status.push (stat); Glib::RefPtr<Glib::MainContext> ctx (Glib::MainContext::get_default ()); while (ctx->iteration (false)); // Update statusbar pid_t pid (-1); int pipes[2]; try { setenv ("LANGUAGE", lang.c_str (), true); args[POS_LANG] = lang.c_str (); TRACE3 ("CDManager::export2HTML () - Parms: " << args[POS_LANG] << ' ' << args[POS_LANG + 1]); pipe (pipes); pid = YGP::Process::execIOConnected ("CDWriter", args, pipes); for (unsigned int i (0); i < (WITH_RECORDS + WITH_MOVIES); ++i) pages[i]->export2HTML (pipes[1], lang); close (pipes[1]); char output[128] = ""; std::string allOut; int cRead; while ((cRead = ::read (pipes[0], output, sizeof (output))) != -1) { allOut.append (output, cRead); if (!cRead) break; } if (allOut.size ()) { Gtk::MessageDialog dlg (Glib::locale_to_utf8 (output), Gtk::MESSAGE_INFO); dlg.set_title (_("Export Warning!")); dlg.run (); } Check3 (pid != -1); YGP::Process::waitForProcess (pid); } catch (std::exception& e) { Gtk::MessageDialog dlg (e.what (), Gtk::MESSAGE_ERROR); dlg.run (); } close (pipes[0]); status.pop (); } // end-while setenv ("LANGUAGE", oldLang.c_str (), true);}#endif#if WITH_RECORDS == 1//-----------------------------------------------------------------------------/// Reads the ID3 information from a MP3 file/// \param file: Name of file to analzye//-----------------------------------------------------------------------------void CDManager::parseFileInfo (const std::string& file) { TRACE8 ("CDManager::parseFileInfo (const std::string&) - " << file); Check2 (file.size ()); std::ifstream stream (file.c_str ()); Glib::ustring artist, record, song; unsigned int track (0); if (!stream) { Glib::ustring err (_("Can't open file `%1'!\n\nReason: %2")); err.replace (err.find ("%1"), 2, file); err.replace (err.find ("%2"), 2, strerror (errno)); Gtk::MessageDialog (err).run (); return; } TRACE1 ("CDManager::parseFileInfo (const std::string&) - Type: " << file.substr (file.size () - 4)); if (((file.substr (file.size () - 4) == ".mp3") && parseMP3Info (stream, artist, record, song, track)) || ((file.substr (file.size () - 4) == ".ogg") && parseOGGCommentHeader (stream, artist, record, song, track))) { TRACE8 ("CDManager::parseFileInfo (const std::string&) - " << artist << '/' << record << '/' << song << '/' << track); Check1 (typeid (**pages) == typeid (PRecords)); ((PRecords*)*pages)->addEntry (artist, record, song, track); }}//-----------------------------------------------------------------------------/// Reads the ID3 information from a MP3 file/// \param stream: MP3-file to analyze/// \param artist: Found artist/// \param record: Found record name/// \param song: Found song/// \param track: Tracknumber/// \returns bool: True, if ID3 info has been found//-----------------------------------------------------------------------------bool CDManager::parseMP3Info (std::istream& stream, Glib::ustring& artist, Glib::ustring& record, Glib::ustring& song, unsigned int& track) { stream.seekg (-0x80, std::ios::end); std::string value; std::getline (stream, value, '\xff'); TRACE8 ("CDManager::parseMP3Info (std::istream&, 3x Glib::ustring&, unsigned&) - Found: " << value << "; Length: " << value.size ()); if ((value.size () > 3) && (value[0] == 'T') && (value[1] == 'A') && (value[2] == 'G')) { song = Glib::locale_to_utf8 (stripString (value, 3, 29)); artist = Glib::locale_to_utf8 (stripString (value, 33, 29)); record = Glib::locale_to_utf8 (stripString (value, 63, 29)); track = (value[0x7d] != 0x20) ? value[0x7e] : 0; return true; } return false;}//-----------------------------------------------------------------------------/// Reads the OGG comment header from an OGG vorbis encoded file/// \param stream: OGG-file to analyze/// \param artist: Found artist/// \param record: Found record name/// \param song: Found song/// \param track: Tracknumber/// \returns bool: True, if comment header has been found//-----------------------------------------------------------------------------bool CDManager::parseOGGCommentHeader (std::istream& stream, Glib::ustring& artist, Glib::ustring& record, Glib::ustring& song, unsigned int& track) { char buffer[512]; stream.read (buffer, 4); if ((*buffer != 'O') && (buffer[1] != 'g') && (buffer[2] != 'g') && (buffer[3] != 'S')) return false; stream.seekg (0x69, std::ios::cur); unsigned int len (0); stream.read ((char*)&len, 4); // Read the vendorstring-length TRACE8 ("CDManager::parseOGGCommentHeader (std::istream&, 3x Glib::ustring&, unsigned&) - Length: " << len); stream.seekg (len, std::ios::cur); unsigned int cComments (0); stream.read ((char*)&cComments, 4); // Read number of comments TRACE8 ("CDManager::parseOGGCommentHeader (std::istream&, 3x Glib::ustring&, unsigned&) - Comments: " << cComments); if (!cComments) return false; std::string key; Glib::ustring *value (NULL); do { stream.read ((char*)&len, 4); // Read the comment-length std::getline (stream, key, '='); len -= key.size () + 1; TRACE8 ("CDManager::parseOGGCommentHeader (std::stream&, 3x Glib::ustring&, unsigned&) - Key: " << key); if (key == "TITLE") value = &song; else if (key == "ALBUM") value = &record; else if (key == "ARTIST") value = &artist; else if (key == "TRACKNUMBER") { Check2 (len < sizeof (buffer)); stream.read (buffer, len); track = strtoul (buffer, NULL, 10); value = NULL; len = 0; } else value = NULL; if (value) { unsigned int read (0); do { read = stream.readsome (buffer, (len > sizeof (buffer)) ? sizeof (buffer) - 1 : len); len -= read; buffer[read] = '\0'; value->append (buffer); } while (len); } else stream.seekg (len, std::ios::cur); } while (--cComments); // end-do while comments return true;}//-----------------------------------------------------------------------------/// Returns the specified substring, removed from trailing spaces/// \param value: String to manipulate/// \param pos: Starting pos inside the string/// \param len: Maximal length of string/// \returns std::string: Stripped value//-----------------------------------------------------------------------------std::string CDManager::stripString (const std::string& value, unsigned int pos, unsigned int len) { len += pos; while (len > pos) { if ((value[len] != ' ') && (value[len])) break; --len; } return (pos == len) ? " " : value.substr (pos, len - pos + 1);}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -