00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00058 slotFileSave();
00059 }
00060
00061 if (Prefs::autoBackup())
00062 QTimer::singleShot(Prefs::backupTime() * 60 * 1000, this, SLOT(slotTimeOutBackup()));
00063
00064 }
00065
00066
00067 bool ParleyApp::queryClose()
00068 {
00069 bool erg = queryExit();
00070 if (erg)
00071 m_doc->setModified(false);
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();
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;
00089 } else if (exit == KMessageBox::No) {
00090 save = false;
00091 } else {
00092 return false;
00093 }
00094 }
00095
00096 if (save) {
00097 if (!m_doc->url().isEmpty())
00098 slotFileSave();
00099 if (m_doc->isModified()) {
00100
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
00128 }
00129
00130 void ParleyApp::slotFileOpen()
00131 {
00132
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
00140 }
00141
00142 void ParleyApp::slotFileOpenRecent(const KUrl& url)
00143 {
00144
00145 if (queryExit()) {
00146 loadFileFromPath(url);
00147 }
00148
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
00159
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) {
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
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
00197 }
00198
00199
00200 void ParleyApp::slotGHNS()
00201 {
00202 KNS::Entry::List entries = KNS::Engine::download();
00203
00204 foreach(KNS::Entry* entry, entries) {
00205
00206 if (entry->status() == KNS::Entry::Installed) {
00207
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
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
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
00266
00267
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
00284 }
00285
00286
00287 void ParleyApp::slotFileSaveAs()
00288 {
00289
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
00303 } else
00304
00305 if (m_doc) {
00306 QString msg = i18nc("@info:status saving a file", "Saving %1", url.path());
00307
00308
00309 QFile::remove(QFile::encodeName(url.path()+'~'));
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
00329 }
00330
00331
00332 void ParleyApp::slotSaveSelection()
00333 {
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
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();
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
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
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();
00483
00484
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
00505 m_tableModel->loadLanguageSettings();
00506
00507 int lessonIndex = m_lessonModel->addLesson();
00508
00509 if (m_lessonView) {
00510 m_lessonView->slotSelectLesson(lessonIndex);
00511 }
00512
00513
00514 for ( int i = 0; i < 15 ; i++ ) {
00515 m_tableModel->appendEntry();
00516 }
00517
00518
00519 Prefs::setCurrentCol(KV_COL_TRANS);
00520 Prefs::setCurrentRow(0);
00521
00522 m_doc->setModified(false);
00523 }
00524
00525