18 #include "../config-parley.h"
28 #include <keduvoclesson.h>
29 #include <keduvocleitnerbox.h>
30 #include <keduvocexpression.h>
31 #include <keduvocwordtype.h>
33 #include <KFileDialog>
34 #include <KRecentFilesAction>
35 #include <KStandardDirs>
36 #include <knewstuff3/downloaddialog.h>
37 #include <knewstuff3/uploaddialog.h>
38 #include <KEMailSettings>
39 #include <KMessageBox>
44 #include <QtGui/QPrinter>
45 #include <QtGui/QPrintDialog>
49 #include <libxml/parser.h>
50 #include <libxml/tree.h>
51 #include <libxslt/xslt.h>
52 #include <libxslt/xsltInternals.h>
53 #include <libxslt/transform.h>
54 #include <libxslt/xsltutils.h>
60 namespace DocumentHelper
64 QString locale = doc->identifier(languageIndex).locale();
66 KUrl location(
QString(
"http://edu.kde.org/parley/locale/") + locale +
QString(
".kvtml"));
68 KEduVocDocument grammarDoc;
69 if (grammarDoc.open(location) == KEduVocDocument::NoError) {
70 doc->identifier(languageIndex).setArticle(grammarDoc.identifier(0).article());
71 doc->identifier(languageIndex).setPersonalPronouns(grammarDoc.identifier(0).personalPronouns());
73 doc->identifier(languageIndex).setTenseList(grammarDoc.identifier(0).tenseList());
75 kDebug() <<
"Download of " << location.url() <<
" failed.";
82 , m_parleyApp(parleyMainWindow)
84 , m_doc(new KEduVocDocument(this))
101 m_doc =
new KEduVocDocument();
109 m_doc->setTitle(title);
121 KEduVocDocument *newDoc =
new KEduVocDocument();
123 initializeDefaultGrammar(newDoc);
124 setDefaultDocumentProperties(newDoc);
125 bool showGrammarDialog =
false;
126 bool fetchGrammarOnline =
false;
130 titleAuthorDialog =
new KDialog(m_parleyApp);
131 titleAuthorDialog->setMainWidget(titleAuthorWidget);
132 titleAuthorDialog->setCaption(i18nc(
"@title:window document properties",
"Properties for %1", newDoc->url().url()));
133 connect(titleAuthorDialog, SIGNAL(accepted()), titleAuthorWidget, SLOT(accept()));
134 if (titleAuthorDialog->exec()) {
135 showGrammarDialog = titleAuthorWidget->grammarCheckBox->isChecked();
136 fetchGrammarOnline = titleAuthorWidget->downloadGrammarCheckBox->isChecked();
137 delete titleAuthorDialog;
139 delete titleAuthorDialog;
147 m_doc->deleteLater();
153 if (fetchGrammarOnline) {
157 if (showGrammarDialog) {
169 KFileDialog dialog(
QString(), KEduVocDocument::pattern(KEduVocDocument::Reading), m_parleyApp, practiceCheckBox);
170 dialog.setCaption(i18n(
"Open Vocabulary Collection"));
172 && !dialog.selectedUrl().isEmpty()
173 &&
open(dialog.selectedUrl())
193 if (url.path().isEmpty()) {
200 m_doc->deleteLater();
202 m_doc =
new KEduVocDocument(
this);
206 bool isSuccess =
false, isError =
false;
208 KEduVocDocument::ErrorCode ret = m_doc->open(url, KEduVocDocument::FileDefaultHandling);
210 case KEduVocDocument::NoError :
213 case KEduVocDocument::FileLocked :
215 int exit = KMessageBox::warningYesNo(
216 m_parleyApp, i18n(
"The vocabulary collection is locked by another process. You can open the file if you take over the lock, but you will lose any changes from the other process.\n\nDo you want to take over the lock?\n"), i18n(
"Take Over Lock" ) ) ;
217 if ( exit == KMessageBox::Yes ) {
219 ret = m_doc->open(url, KEduVocDocument::FileIgnoreLock);
220 if ( ret == KEduVocDocument::NoError ) {
221 kDebug() <<
"Lock stolen";
243 m_parleyApp, i18n(
"Opening collection \"%1\" resulted in an error: %2", m_doc->url().url(),
244 m_doc->errorDescription(ret)), i18nc(
"@title:window",
"Open Collection"));
246 m_doc->deleteLater();
259 m_doc->deleteLater();
266 if ( (m_doc == NULL ) || !m_doc->isModified()) {
275 int exit = KMessageBox::warningYesNoCancel(
276 m_parleyApp, i18n(
"Vocabulary is modified.\n\nSave file before exit?\n"),
277 "", KStandardGuiItem::save(), KStandardGuiItem::discard());
279 case KMessageBox::Yes :
282 case KMessageBox::No :
285 case KMessageBox::Continue :
303 QString downloadDir = KStandardDirs::locateLocal(
"data",
"kvtml/");
304 KUrl url = KFileDialog::getOpenUrl(
306 KEduVocDocument::pattern(KEduVocDocument::Reading),
308 i18n(
"Open Downloaded Vocabulary Collection"));
317 if (m_doc->url().fileName() == i18n(
"Untitled")) {
331 m_doc->setGenerator(newgenerator);
333 bool isSuccess =
false, isError = false ;
335 KEduVocDocument::ErrorCode ret = m_doc->saveAs(
336 m_doc->url() , KEduVocDocument::Automatic, KEduVocDocument::FileIgnoreLock);
339 case KEduVocDocument::NoError :
342 case KEduVocDocument::FileLocked :
344 int exit = KMessageBox::warningYesNo(
345 m_parleyApp, i18n(
"File \"%1\" is locked by another process. You can save to the file if you take over the lock, but you will lose any changes from the other process.\n\nDo you want to take over the lock?\n"
346 , m_doc->url().url()),
"");
347 if ( exit == KMessageBox::Yes ) {
348 m_doc->setGenerator(newgenerator );
349 ret = m_doc->saveAs(m_doc->url() , KEduVocDocument::Automatic, KEduVocDocument::FileIgnoreLock);
351 if ( ret == KEduVocDocument::NoError ) {
353 kDebug() <<
"Lock stolen";
372 m_parleyApp, i18n(
"Writing file \"%1\" resulted in an error: %2", m_doc->url().url(),
373 m_doc->errorDescription(ret)), i18nc(
"@title:window",
"Save File"));
375 kDebug() <<
"Save failed trying save as for "<< m_doc->url().url();
388 url = KFileDialog::getSaveUrl(
QString(),
389 KEduVocDocument::pattern(KEduVocDocument::Writing),
390 m_parleyApp->parentWidget(),
391 i18n(
"Save Vocabulary As"));
398 if (fileinfo.exists()) {
399 if (KMessageBox::warningContinueCancel(
400 0, i18n(
"<qt>The file<p><b>%1</b></p>already exists. Do you want to overwrite it?</qt>",
401 url.toLocalFile()),
QString(), KStandardGuiItem::overwrite()) == KMessageBox::Cancel) {
406 QString msg = i18nc(
"@info:status saving a file",
"Saving %1", url.toLocalFile());
413 if (!url.fileName().contains(
'.')) {
417 bool isSuccess =
false, isError =
false;
418 int ret = m_doc->saveAs(url, KEduVocDocument::Automatic,
"Parley");
420 case KEduVocDocument::NoError :
423 case KEduVocDocument::FileLocked :
425 int exit = KMessageBox::warningYesNo(
426 m_parleyApp, i18n(
"File \"%1\" is locked by another process. You can save to the file if you take over the lock, but you will lose any changes from the other process.\n\nDo you want to take over the lock?\n"
427 , m_doc->url().url()),
"");
428 if ( exit == KMessageBox::Yes ) {
431 m_doc->url() , KEduVocDocument::Automatic, KEduVocDocument::FileIgnoreLock);
433 if ( ret == KEduVocDocument::NoError ) {
435 kDebug() <<
"Lock stolen";
451 kDebug() <<
"SaveAs success.";
456 kDebug() <<
"SaveAs failed for "<< m_doc->url().url()<<
" \nwhy "<< m_doc->errorDescription(ret);
459 m_parleyApp, i18n(
"Writing file \"%1\" resulted in an error: %2",
460 m_doc->url().url(), m_doc->errorDescription(ret)), i18nc(
"@title:window",
"Save File"));
467 void ParleyDocument::initializeDefaultGrammar(KEduVocDocument *doc)
469 KEduVocWordType *root = doc->wordTypeContainer();
470 KEduVocWordType *noun =
new KEduVocWordType(i18n(
"Noun"), root);
471 noun->setWordType(KEduVocWordFlag::Noun);
472 root->appendChildContainer(noun);
474 KEduVocWordType *nounChild =
new KEduVocWordType(i18n(
"Masculine"), noun);
475 nounChild->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Masculine);
476 noun->appendChildContainer(nounChild);
477 nounChild =
new KEduVocWordType(i18n(
"Feminine"), noun);
478 nounChild->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Feminine);
479 noun->appendChildContainer(nounChild);
480 nounChild =
new KEduVocWordType(i18n(
"Neuter"), noun);
481 nounChild->setWordType(KEduVocWordFlag::Noun | KEduVocWordFlag::Neuter);
482 noun->appendChildContainer(nounChild);
484 KEduVocWordType *verb =
new KEduVocWordType(i18n(
"Verb"), root);
485 verb->setWordType(KEduVocWordFlag::Verb);
486 root->appendChildContainer(verb);
488 KEduVocWordType *adjective =
new KEduVocWordType(i18n(
"Adjective"), root);
489 adjective->setWordType(KEduVocWordFlag::Adjective);
490 root->appendChildContainer(adjective);
492 KEduVocWordType *adverb =
new KEduVocWordType(i18n(
"Adverb"), root);
493 adverb->setWordType(KEduVocWordFlag::Adverb);
494 root->appendChildContainer(adverb);
496 KEduVocWordType *conjunction =
new KEduVocWordType(i18n(
"Conjunction"), root);
497 conjunction->setWordType(KEduVocWordFlag::Conjunction);
498 root->appendChildContainer(conjunction);
502 void ParleyDocument::setDefaultDocumentProperties(KEduVocDocument *doc)
504 KEMailSettings emailSettings;
505 emailSettings.setProfile(emailSettings.defaultProfileName());
506 doc->setAuthor(emailSettings.getSetting(KEMailSettings::RealName));
507 doc->setAuthorContact(emailSettings.getSetting(KEMailSettings::EmailAddress));
509 doc->setLicense(i18n(
"Public Domain"));
510 doc->setCategory(i18n(
"Languages"));
512 QString locale = KGlobal::locale()->language();
514 doc->appendIdentifier();
515 doc->appendIdentifier();
516 doc->identifier(0).setName(KGlobal::locale()->languageCodeToName(locale));
517 doc->identifier(0).setLocale(locale);
518 doc->identifier(1).setName(i18n(
"A Second Language"));
519 doc->identifier(1).setLocale(locale);
521 KEduVocLesson* lesson =
new KEduVocLesson(i18n(
"Lesson 1"), doc->lesson());
522 doc->lesson()->appendChildContainer(lesson);
525 for (
int i = 0; i < 15 ; i++) {
526 lesson->appendEntry(
new KEduVocExpression());
529 doc->setModified(
false);
536 newStuffDialog.exec();
537 KNS3::Entry::List entries = newStuffDialog.installedEntries();
538 int numberInstalled = entries.size();
539 foreach (
const KNS3::Entry & entry, entries) {
541 foreach (
const QString & file, entry.installedFiles()) {
542 KMimeType::Ptr mimeType = KMimeType::findByPath(file);
543 kDebug() <<
"KNS2 file of mime type:" << KMimeType::findByPath(file)->name();
544 if (mimeType->is(
"application/x-kvtml")) {
554 if (numberInstalled > 1) {
556 }
else if (numberInstalled == 1) {
557 if (
open(KUrl(fileName)))
560 KMessageBox::error(m_parleyApp, i18n(
"Could not open vocabulary collection \"%1\"", entries.first().name()));
568 titleAuthorDialog =
new KDialog(m_parleyApp);
569 titleAuthorDialog->setMainWidget(titleAuthorWidget);
572 titleAuthorWidget->languageGroupBox->
setVisible(
false);
573 titleAuthorDialog->setCaption(i18nc(
"@title:window document properties",
"Properties for %1", m_doc->url().url()));
574 connect(titleAuthorDialog, SIGNAL(accepted()), titleAuthorWidget, SLOT(accept()));
575 titleAuthorDialog->exec();
576 delete titleAuthorDialog;
583 if (properties.exec() == KDialog::Accepted) {
592 KUrl url(dir.name() + m_doc->url().fileName());
593 kDebug() <<
"save in " << url.url();
594 if ( m_doc->saveAs(url, KEduVocDocument::Automatic,
"Parley") != KEduVocDocument::NoError ) {
595 KMessageBox::error(m_parleyApp, i18n(
"Could not save vocabulary collection \"%1\"", url.fileName() ));
599 KEduVocDocument tempDoc(
this);
600 if ( tempDoc.open(url) != KEduVocDocument::NoError ) {
601 KMessageBox::error(m_parleyApp, i18n(
"Could not open vocabulary collection \"%1\"", url.fileName() ));
606 tempDoc.lesson()->resetGrades(-1, KEduVocContainer::Recursive);
607 if ( tempDoc.saveAs(url, KEduVocDocument::Automatic,
"Parley") != KEduVocDocument::NoError ) {
608 KMessageBox::error(m_parleyApp, i18n(
"Could not save vocabulary collection \"%1\"", url.fileName() ));
614 dialog.setUploadFile(url);
657 m_backupTimer->
stop();
660 if (!m_backupTimer) {
661 m_backupTimer =
new QTimer(
this);
662 connect(m_backupTimer, SIGNAL(timeout()),
this, SLOT(
save()));
668 #include "parleydocument.moc"
ParleyDocument(ParleyMainWindow *parleyMainWindow)
Component currentComponent()
void documentChanged(KEduVocDocument *newDocument)
Emitted when the document pointer is changed.
#define PARLEY_VERSION_STRING
void languageProperties()
void setTitle(const QString &title)
static bool autoSave()
Get If true, vocabularies are automatically saved on close and exit.
bool queryClose()
When quitting, ask for confirmation if the doc has not been saved.
void documentProperties()
General doc properties like title, author etc.
void slotFileMerge()
merge a document
void fetchGrammar(KEduVocDocument *doc, int languageIndex)
void slotFileOpenRecent(const KUrl &url)
opens a file from the recent files menu
KEduVocDocument * document()
bool queryClose()
When quitting, ask for confirmation if the doc has not been saved.
void slotFileOpen()
open a document
bool copy(const QString &newName)
void save()
save a document
void newDocument(bool wizard)
static ParleyMainWindow * instance()
void showPracticeConfiguration()
void enableAutoBackup(bool enable)
Enable/disable the timed auto backup.
void openGHNS()
open a downloaded (knewstuff/get hot new stuff) document
void uploadFile()
upload the current file
static int backupTime()
Get Time interval between two automatic backups.
void slotGHNS()
download new vocabularies
void updateRecentFilesModel()
update the list of recent files in the dashboard
static bool autoBackup()
Get If true, a backup is saved every BackupTime minutes.
QString fromLatin1(const char *str, int size)
void addRecentFile(const KUrl &url, const QString &name)
add a new entry to the list of recent files
bool open(const KUrl &)
Opens the given url, displays an error message and returns false on failure.
bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *method, Qt::ConnectionType type)
void close()
close the document
void saveAs(KUrl file=KUrl())
void slotFileNew()
open a new application window
QByteArray encodeName(const QString &fileName)
static QString separator()
Get This sets the separator used when copying/pasting text, default is Tab.