• Skip to content
  • Skip to link menu
KDE 4.0 API Reference
  • KDE API Reference
  • kdeedu
  • Sitemap
  • Contact Us
 

parley

kva_io.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 
00003                          i/o part of kvoctrain
00004 
00005     -----------------------------------------------------------------------
00006 
00007     begin         : Thu Mar 11 20:50:53 MET 1999
00008 
00009     copyright     : (C) 1999-2001 Ewald Arnold <kvoctrain@ewald-arnold.de>
00010                     (C) 2004-2007 Peter Hedlund <peter.hedlund@kdemail.net>
00011                     (C) 2007 Frederik Gladhorn <frederik.gladhorn@kdemail.net>
00012 
00013     -----------------------------------------------------------------------
00014 
00015  ***************************************************************************/
00016 
00017 /***************************************************************************
00018  *                                                                         *
00019  *   This program is free software; you can redistribute it and/or modify  *
00020  *   it under the terms of the GNU General Public License as published by  *
00021  *   the Free Software Foundation; either version 2 of the License, or     *
00022  *   (at your option) any later version.                                   *
00023  *                                                                         *
00024  ***************************************************************************/
00025 
00026 #include "parley.h"
00027 #include "newdocument-wizard/kvtnewdocumentwizard.h"
00028 #include "entry-dialogs/EntryDlg.h"
00029 #include "prefs.h"
00030 
00031 #include "kvttablemodel.h"
00032 #include "kvtsortfiltermodel.h"
00033 #include "kvttableview.h"
00034 #include "kvtlessonmodel.h"
00035 #include "kvtlessonview.h"
00036 
00037 #include <KFileDialog>
00038 #include <KStatusBar>
00039 #include <KRecentFilesAction>
00040 #include <KStandardDirs>
00041 #include <knewstuff2/engine.h>
00042 #include <KUser>
00043 #include <KMessageBox>
00044 #include <KProcess>
00045 
00046 #include <KDebug>
00047 #include <KComponentData>
00048 #include <QTimer>
00049 #include <QFrame>
00050 #include <QWizard>
00051 #include <QApplication>
00052 #include <QProgressBar>
00053 
00054 void ParleyApp::slotTimeOutBackup()
00055 {
00056     if (Prefs::autoBackup() && m_doc && m_doc->isModified()) {
00057 //         slotStatusMsg(i18n("Autobackup in progress"));
00058         slotFileSave();
00059     }
00060 
00061     if (Prefs::autoBackup())
00062         QTimer::singleShot(Prefs::backupTime() * 60 * 1000, this, SLOT(slotTimeOutBackup()));
00063 //     slotStatusMsg(IDS_DEFAULT);
00064 }
00065 
00066 
00067 bool ParleyApp::queryClose()
00068 {
00069     bool erg = queryExit();
00070     if (erg)
00071         m_doc->setModified(false);  // avoid double query on exit via system menu
00072     return erg;
00073 }
00074 
00075 
00076 bool ParleyApp::queryExit()
00077 {
00078     saveOptions();
00079     if (!m_doc || !m_doc->isModified())
00080         return true;
00081 
00082     bool save = Prefs::autoSave(); //save without asking
00083 
00084     if (!save) {
00085         int exit = KMessageBox::warningYesNoCancel(this, i18n("Vocabulary is modified.\n\nSave file before exit?\n"),
00086                    "", KStandardGuiItem::save(), KStandardGuiItem::discard());
00087         if (exit == KMessageBox::Yes) {
00088             save = true;   // save and exit
00089         } else if (exit == KMessageBox::No) {
00090             save = false;  // don't save but exit
00091         } else {
00092             return false;  // continue work
00093         }
00094     }
00095 
00096     if (save) {
00097         if (!m_doc->url().isEmpty())
00098             slotFileSave();       // save and exit
00099         if (m_doc->isModified()) {
00100             // Error while saving or no name
00101             slotFileSaveAs();
00102         }
00103     }
00104     return true;
00105 }
00106 
00107 
00108 void ParleyApp::slotFileQuit()
00109 {
00110     close();
00111 }
00112 
00113 
00114 void ParleyApp::slotProgress(KEduVocDocument *curr_doc, int percent)
00115 {
00116     Q_UNUSED(curr_doc);
00117     if (pbar != 0)
00118         pbar->setValue(percent);
00119     qApp->processEvents();
00120 }
00121 
00122 void ParleyApp::slotFileNew()
00123 {
00124     if (queryExit()) {
00125         newDocumentWizard();
00126     }
00127 //     slotStatusMsg(IDS_DEFAULT);
00128 }
00129 
00130 void ParleyApp::slotFileOpen()
00131 {
00132 //     slotStatusMsg(i18n("Opening file..."));
00133 
00134     if (queryExit()) {
00135         KUrl url = KFileDialog::getOpenUrl(QString(), KEduVocDocument::pattern(KEduVocDocument::Reading), this, i18n("Open Vocabulary Document"));
00136         loadFileFromPath(url, true);
00137     }
00138 
00139 //     slotStatusMsg(IDS_DEFAULT);
00140 }
00141 
00142 void ParleyApp::slotFileOpenRecent(const KUrl& url)
00143 {
00144 //     slotStatusMsg(i18n("Opening file..."));
00145     if (queryExit()) {
00146         loadFileFromPath(url);
00147     }
00148 //     slotStatusMsg(IDS_DEFAULT);
00149 }
00150 
00151 
00152 void ParleyApp::loadFileFromPath(const KUrl & url, bool addRecent)
00153 {
00154     if (!url.path().isEmpty()) {
00155         delete m_doc;
00156         m_doc = 0;
00157 
00158 //         slotStatusMsg(i18n("Loading %1", url.path()));
00159         //prepareProgressBar();
00160         m_doc = new KEduVocDocument(this);
00161         m_doc->setCsvDelimiter(Prefs::separator());
00162         m_doc->open(url);
00163 
00164         m_tableModel->setDocument(m_doc);
00165         m_tableModel->reset();
00166         m_tableModel->loadLanguageSettings();
00167 
00168         removeProgressBar();
00169         if (addRecent) { // open sample does not go into recent
00170             fileOpenRecent->addUrl(url);
00171         }
00172         connect(m_doc, SIGNAL(docModified(bool)), this, SLOT(slotUpdateWindowCaption()));
00173         m_doc->setModified(false);
00174         m_sortFilterModel->restoreNativeOrder();
00175         if (m_tableView) {
00176             m_tableView->adjustContent();
00177         }
00178         m_lessonModel->setDocument(m_doc);
00179     }
00180 }
00181 
00182 
00183 void ParleyApp::slotFileOpenExample()
00184 {
00185 //     slotStatusMsg(i18n("Opening example file..."));
00186 
00187     if (queryExit()) {
00188         QString s = KStandardDirs::locate("data", "parley/examples/");
00189         KUrl url = KFileDialog::getOpenUrl(s, KEduVocDocument::pattern(KEduVocDocument::Reading), this, i18n("Open Example Vocabulary Document"));
00190         loadFileFromPath(url, false);
00191         if (m_doc) {
00192             m_doc->url().setFileName(QString());
00193         }
00194     }
00195 
00196 //     slotStatusMsg(IDS_DEFAULT);
00197 }
00198 
00199 
00200 void ParleyApp::slotGHNS()
00201 {
00202     KNS::Entry::List entries = KNS::Engine::download();
00203     // list of changed entries
00204     foreach(KNS::Entry* entry, entries) {
00205         // care only about installed ones
00206         if (entry->status() == KNS::Entry::Installed) {
00207             // check mime type and if kvtml, open it
00208             foreach(QString file, entry->installedFiles()) {
00209                 KMimeType::Ptr mimeType = KMimeType::findByPath(file);
00210                 kDebug() << "KNS2 file of mime type:" << KMimeType::findByPath(file)->name();
00211                 if (mimeType->name() == "application/x-kvtml") {
00212                     KProcess newParley;
00213                     newParley.setProgram(m_appName, QStringList() << file);
00214                     newParley.startDetached();
00215                 }
00216             }
00217         }
00218     }
00219     qDeleteAll(entries);
00220 }
00221 
00222 
00223 void ParleyApp::slotFileMerge()
00224 {
00225 //     slotStatusMsg(i18n("Merging file..."));
00226 //
00227 //     KUrl url = KFileDialog::getOpenUrl(QString(), KEduVocDocument::pattern(KEduVocDocument::Reading), parentWidget(), i18n("Merge Vocabulary File"));
00228 //
00229 //     if (!url.isEmpty()) {
00230 //         QString msg = i18n("Loading %1", url.path());
00231 //         slotStatusMsg(msg);
00232 //
00233 //         KEduVocDocument *new_doc = new KEduVocDocument(this);
00234 //         new_doc->setCsvDelimiter(Prefs::separator());
00235 //         new_doc->open(url);
00236 //
00237 //         m_doc->merge(new_doc, true);
00238 //
00239 //         KEduVocConjugation::setTenseNames(m_doc->tenseDescriptions());
00240 //         KVTUsage::setUsageNames(m_doc->usageDescriptions());
00241 //
00242 //         delete(new_doc);
00243 //         fileOpenRecent->addUrl(url);
00244 //         m_tableModel->reset();
00245 //         m_lessonModel->setDocument(m_doc);
00246 //         m_tableView->adjustContent();
00247 //     }
00248 //
00249 //     slotStatusMsg(IDS_DEFAULT);
00250 }
00251 
00252 
00253 void ParleyApp::slotFileSave()
00254 {
00255     if (entryDlg != 0) {
00256         entryDlg->commitData(false);
00257     }
00258 
00259     if (m_doc->url().fileName() == i18n("Untitled")) {
00260         slotFileSaveAs();
00261         return;
00262     }
00263 
00264     QString msg = i18nc("@info:status saving a file", "Saving %1", m_doc->url().path());
00265 //     slotStatusMsg(msg);
00266 
00267     // remove previous backup
00268     QFile::remove(QFile::encodeName(m_doc->url().path()+'~'));
00269     ::rename(QFile::encodeName(m_doc->url().path()), QFile::encodeName(m_doc->url().path()+'~'));
00270 
00271     prepareProgressBar();
00272     m_doc->setCsvDelimiter(Prefs::separator());
00273 
00274     int result = m_doc->saveAs(m_doc->url(), KEduVocDocument::Automatic, "Parley");
00275     if ( result != 0 ) {
00276         KMessageBox::error(this, i18n("Writing file \"%1\" resulted in an error: %2", m_doc->url().url(), m_doc->errorDescription(result)), i18n("Save File"));
00277         slotFileSaveAs();
00278         return;
00279     }
00280     fileOpenRecent->addUrl(m_doc->url());
00281     removeProgressBar();
00282 
00283 //     slotStatusMsg(IDS_DEFAULT);
00284 }
00285 
00286 
00287 void ParleyApp::slotFileSaveAs()
00288 {
00289 //     slotStatusMsg(i18n("Saving file under new filename..."));
00290 
00291     if (entryDlg != 0) {
00292         entryDlg->commitData(false);
00293     }
00294 
00295     KUrl url = KFileDialog::getSaveUrl(QString(), KEduVocDocument::pattern(KEduVocDocument::Writing), parentWidget(), i18n("Save Vocabulary As"));
00296 
00297     if (!url.isEmpty()) {
00298         QFileInfo fileinfo(url.path());
00299         if (fileinfo.exists() && KMessageBox::warningContinueCancel(0,
00300                 i18n("<qt>The file<p><b>%1</b></p>already exists. Do you want to overwrite it?</qt>",
00301                      url.path()),QString(),KStandardGuiItem::overwrite()) == KMessageBox::Cancel) {
00302             // do nothing
00303         } else
00304 
00305             if (m_doc) {
00306                 QString msg = i18nc("@info:status saving a file", "Saving %1", url.path());
00307 //                 slotStatusMsg(msg);
00308 
00309                 QFile::remove(QFile::encodeName(url.path()+'~')); // remove previous backup
00310                 ::rename(QFile::encodeName(url.path()), QFile::encodeName(QString(url.path()+'~')));
00311 
00312                 prepareProgressBar();
00313                 m_doc->setCsvDelimiter(Prefs::separator());
00314 
00315                 if ( !url.fileName().contains('.') ) {
00316                     url.setFileName(url.fileName() + QString::fromLatin1(".kvtml"));
00317                 }
00318 
00319                 int result = m_doc->saveAs(url, KEduVocDocument::Automatic, "Parley");
00320                 if (result != 0) {
00321                     KMessageBox::error(this, i18n("Writing file \"%1\" resulted in an error: %2", m_doc->url().url(), m_doc->errorDescription(result)), i18n("Save File"));
00322                 } else {
00323                     fileOpenRecent->addUrl(m_doc->url());
00324                     removeProgressBar();
00325                 }
00326             }
00327     }
00328 //     slotStatusMsg(IDS_DEFAULT);
00329 }
00330 
00331 
00332 void ParleyApp::slotSaveSelection()
00333 {
00335 
00336     /*
00337     if (entryDlg != 0) {
00338         entryDlg->commitData(false);
00339     }
00340 //     slotStatusMsg(i18n("Saving selected area under new filename..."));
00341     QString save_separator = Prefs::separator();
00342     Prefs::setSeparator("\t");
00343     KEduVocDocument seldoc(this);
00344     // transfer most important parts
00345     for (int i = 0; i < m_doc->identifierCount(); i++) {
00346         seldoc.appendIdentifier(m_doc->identifier(i));
00347     }
00348     seldoc.setAuthor(m_doc->author());
00349     //seldoc.setLessons(m_doc->lessons());
00350     //seldoc.setLessonsInQuery(m_doc->lessonsInQuery());
00351     //seldoc.setTypeDescriptions(m_doc->typeDescriptions());
00352 
00353 //     for (int i = m_doc->entryCount()-1; i >= 0; i--)
00354 //         if (m_doc->entry(i)->isInQuery())
00355 //             seldoc.appendEntry(m_doc->entry(i));
00356 
00357     KUrl url = KFileDialog::getSaveUrl(QString(), KEduVocDocument::pattern(KEduVocDocument::Writing), parentWidget(), i18n("Save Vocabulary As"));
00358 
00359     if (!url.isEmpty()) {
00360         QFileInfo fileinfo(url.path());
00361         if (fileinfo.exists() && KMessageBox::warningContinueCancel(0,
00362                 i18n("<qt>The file<br><b>%1</b><br />already exists. Do you want to overwrite it?</qt>",
00363                      url.path()),QString(),KStandardGuiItem::overwrite()) == KMessageBox::Cancel) {
00364             // do nothing
00365         } else {
00366             QString msg = i18nc("@info:status saving a file", "Saving %1", url.path());
00367 //             slotStatusMsg(msg);
00368 
00369             QFile::remove(url.path()+'~'); // remove previous backup
00370             // FIXME: check error
00371             ::rename(QFile::encodeName(url.path()), QFile::encodeName(url.path()+'~'));
00372 
00373             prepareProgressBar();
00374             seldoc.setCsvDelimiter(Prefs::separator());
00375             seldoc.saveAs(url, KEduVocDocument::Automatic, "Parley");
00376             removeProgressBar();
00377         }
00378     }
00379     Prefs::setSeparator(save_separator);
00380 //     slotStatusMsg(IDS_DEFAULT);
00381     */
00382 }
00383 
00384 
00385 void ParleyApp::prepareProgressBar()
00386 {
00387     statusBar()->clearMessage();
00388     pbar = new QProgressBar(statusBar());
00389     statusBar()->addPermanentWidget(pbar, 150);
00390     pbar->show();
00391 }
00392 
00393 
00394 void ParleyApp::removeProgressBar()
00395 {
00396     statusBar()->clearMessage();
00397     statusBar()->removeWidget(pbar);
00398     delete pbar;
00399     pbar = 0;
00400 }
00401 
00402 
00403 void ParleyApp::newDocumentWizard()
00404 {
00405     KVTNewDocumentWizard *wizard;
00406     KEduVocDocument* newDoc = new KEduVocDocument(this);
00407 
00408     wizard = new KVTNewDocumentWizard(newDoc, this);
00409     if( !wizard->exec() == KDialog::Accepted ){
00410             delete wizard;
00411             return;
00412     }
00413     delete wizard;
00414 
00415     delete m_doc;
00416     m_doc = 0;
00417     m_doc = newDoc;
00418 
00419     m_tableModel->setDocument(m_doc);
00420     connect(m_doc, SIGNAL(docModified(bool)), this, SLOT(slotUpdateWindowCaption()));
00421 
00422     m_lessonModel->setDocument(m_doc);
00423     if (m_lessonView) {
00424         m_lessonView->setModel(m_lessonModel);
00425         m_lessonView->initializeSelection();
00426     }
00427 
00428     if (m_tableView) {
00429         m_tableView->adjustContent();
00430         m_tableView->setColumnHidden(KV_COL_LESS, !Prefs::tableLessonColumnVisible());
00431         m_tableView->setColumnHidden(KV_COL_MARK, !Prefs::tableActiveColumnVisible());
00432     }
00433 
00434     m_tableModel->reset(); // clear old entries otherwise we get crashes
00435 
00436     initializeDefaultGrammar();
00437 
00438     m_tableModel->loadLanguageSettings();
00439 
00440     int lessonIndex = m_lessonModel->addLesson();
00441 
00442     if (m_lessonView) {
00443         m_lessonView->slotSelectLesson(lessonIndex);
00444     }
00445 
00446     // add some entries
00447     for ( int i = 0; i < 15 ; i++ ) {
00448         m_tableModel->appendEntry();
00449     }
00450 
00451     m_doc->setModified(false);
00452 }
00453 
00454 
00455 void ParleyApp::initializeDefaultGrammar()
00456 {
00457     m_doc->wordTypes().createDefaultWordTypes();
00458 
00459     // Preset some usages
00460     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "abbreviation") );
00461     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "anatomy") );
00462     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "biology") );
00463     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "figuratively") );
00464     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "geology") );
00465     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "historical") );
00466     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "informal") );
00467     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "ironic") );
00468     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "literary") );
00469     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "mythology") );
00470     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "proper name") );
00471     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "pharmacy") );
00472     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "philosophy") );
00473     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "physics") );
00474     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "physiology") );
00475     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "rhetoric") );
00476     m_doc->addUsage( i18nc("context in which vocabulary entry is used", "zoology") );
00477 }
00478 
00479 
00480 void ParleyApp::createExampleEntries()
00481 {
00482     m_tableModel->reset(); // clear old entries otherwise we get crashes
00483 
00484     // some default values
00485     KUser user;
00486     QString userName = user.property(KUser::FullName).toString();
00487     if ( userName.isEmpty() ) {
00488         userName = user.loginName();
00489     }
00490     m_doc->setAuthor( userName );
00491     m_doc->setTitle( i18n("Welcome") );
00492     m_doc->setLicense( i18n("GPL (GNU General Public License)") );
00493     m_doc->setCategory( i18n("Example document") );
00494 
00495     QString locale = KGlobal::locale()->language();
00496 
00497     m_doc->appendIdentifier();
00498     m_doc->appendIdentifier();
00499     m_doc->identifier(0).setName( KGlobal::locale()->languageCodeToName( locale) );
00500     m_doc->identifier(0).setLocale( locale );
00501     m_doc->identifier(1).setName( i18n("A Second Language") );
00502     m_doc->identifier(1).setLocale( locale );
00503 
00504     // Set the language headers of the table.
00505     m_tableModel->loadLanguageSettings();
00506 
00507     int lessonIndex = m_lessonModel->addLesson();
00508 
00509     if (m_lessonView) {
00510         m_lessonView->slotSelectLesson(lessonIndex);
00511     }
00512 
00513     // add some entries
00514     for ( int i = 0; i < 15 ; i++ ) {
00515         m_tableModel->appendEntry();
00516     }
00517 
00518     // select the empty row
00519     Prefs::setCurrentCol(KV_COL_TRANS);
00520     Prefs::setCurrentRow(0);
00521 
00522     m_doc->setModified(false);
00523 }
00524 
00525 

parley

Skip menu "parley"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdeedu

Skip menu "kdeedu"
  • kalzium
  • kanagram
  • kig
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  •   docs
  •   src
  • parley
Generated for kdeedu by doxygen 1.5.4
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal