#include <klocale.h>
#include <kdebug.h>
-#include <kmessagebox.h>
#include <kio/netaccess.h>
#include <krandomsequence.h>
#include <kfilterdev.h>
}
-bool KEduVocDocument::open( const KUrl& url )
+int KEduVocDocument::open( const KUrl& url )
{
d->init();
- if ( !url.isEmpty() )
+ if ( !url.isEmpty() ) {
d->m_url = url;
+ }
bool read = false;
QString errorMessage = i18n( "<qt>Cannot open file<br /><b>%1</b></qt>", url.path() );
QIODevice * f = KFilterDev::deviceForFile( temporaryFile );
if ( !f->open( QIODevice::ReadOnly ) ) {
- KMessageBox::error( 0, errorMessage );
- return false;
+ kError() << errorMessage;
+ return FileCannotRead;
}
FileType ft = detectFileType( url.path() );
if ( !read ) {
QString msg = i18n( "Could not open or properly read \"%1\"\n(Error reported: %2)", url.path(), errorMessage );
- KMessageBox::error( 0, msg, i18n( "Error Opening File" ) );
+ kError() << msg << i18n( "Error Opening File" );
+ ///@todo make the readers return int, pass on the error message properly
+ return FileReaderFailed;
}
f->close();
deleteLesson(defaultLessonNumber, DeleteEmptyLesson);
}
- return read;
+ if ( !read ) {
+ return FileReaderFailed;
+ }
+
+ return 0;
}
-bool KEduVocDocument::saveAs( const KUrl & url, FileType ft, const QString & generator )
+int KEduVocDocument::saveAs( const KUrl & url, FileType ft, const QString & generator )
{
KUrl tmp( url );
QFile f( tmp.path() );
if ( !f.open( QIODevice::WriteOnly ) ) {
- KMessageBox::error( 0, i18n( "<qt>Cannot write to file<br /><b>%1</b></qt>", tmp.path() ) );
- return false;
+ kError() << i18n( "Cannot write to file %1", tmp.path() );
+ return FileCannotWrite;
}
switch ( ft ) {
f.close();
if ( !saved ) {
- QString msg = i18n( "Could not save \"%1\"\nDo you want to try again?", tmp.path() );
- int result = KMessageBox::warningContinueCancel( 0, msg, i18n( "Error Saving File" ), KGuiItem( i18n( "&Retry" ) ) );
- if ( result == KMessageBox::Cancel )
- return false;
+ kError() << "Error Saving File" << tmp.path();
+ return FileWriterFailed;
}
}
d->m_url = tmp;
setModified( false );
- return true;
+ return 0;
}
void KEduVocDocument::merge( KEduVocDocument *docToMerge, bool matchIdentifiers )
}
-QString KEduVocDocument::pattern( Mode mode )
+QString KEduVocDocument::pattern( FileDialogMode mode )
{
static const struct SupportedFilter {
bool reading;
return newfilters.join( "\n" );
}
+QString KEduVocDocument::errorDescription( int errorCode )
+{
+ switch (errorCode) {
+ case NoError:
+ return i18n("No error found.");
+
+ case InvalidXml:
+ return i18n("Invalid XML in document.");
+ case FileTypeUnknown:
+ return i18n("Unknown file type.");
+ case FileCannotWrite:
+ return i18n("File could not be written.");
+ case FileWriterFailed:
+ return i18n("File writer failed.");
+ case FileCannotRead:
+ return i18n("File could not be read.");
+ case FileReaderFailed:
+ return i18n("The file reader failed.");
+ case FileDoesNotExist:
+ return i18n("The file does not exist.");
+ case Unknown:
+ default:
+ return i18n("Unknown error message.");
+ }
+}
KEduVocWordType* KEduVocDocument::wordTypes()
{
Q_OBJECT
public:
- enum FileType
- {
+ /// known vocabulary file types
+ enum FileType {
KvdNone,
Automatic,
Kvtml,
Kvtml1
};
+ /// the return code when opening/saving
+ enum ErrorCode {
+ NoError = 0,
+ Unknown,
+ InvalidXml,
+ FileTypeUnknown,
+ FileCannotWrite,
+ FileWriterFailed,
+ FileCannotRead,
+ FileReaderFailed,
+ FileDoesNotExist
+ };
+
+ /// used as parameter for pattern
+ enum FileDialogMode
+ {
+ Reading,
+ Writing
+ };
+
+ /// delete only empty lessons or also if they have entries
+ enum LessonDeletion
+ {
+ DeleteEmptyLesson,
+ DeleteEntriesAndLesson
+ };
+
/**
* Constructor for a KdeEdu vocabulary document
*
* Open a document file
*
* @param url url to file to open
- * @returns true if successful
+ * @returns ErrorCode
*/
- bool open( const KUrl& url );
+ int open( const KUrl& url );
/**
* Saves the data under the given name
* @param url if url is empty (or NULL) actual name is preserved
* @param ft the filetype to be used when saving the document
* @param generator the name of the application saving the document
- * @returns true if successful
+ * @returns ErrorCode
*/
- bool saveAs( const KUrl & url, FileType ft, const QString & generator );
+ int saveAs( const KUrl & url, FileType ft, const QString & generator );
/**
* Merges data from another document
static FileType detectFileType( const QString &fileName );
- enum Mode
- {
- Reading,
- Writing
- };
-
- enum LessonDeletion
- {
- DeleteEmptyLesson,
- DeleteEntriesAndLesson
- };
-
/**
* Create a string with the supported document types, that can be used
* as filter in KFileDialog. It includes also an entry to match all the
* @param mode the mode for the supported document types
* @returns the filter string
*/
- static QString pattern( Mode mode );
+ static QString pattern( FileDialogMode mode );
+
+ static QString errorDescription( int errorCode );
Q_SIGNALS:
void progressChanged( KEduVocDocument *, int curr_percent );
#include "../keduvocdocument.h"
+#include <KTemporaryFile>
+
#include <qtest_kde.h>
#include <qobject.h>
void KEduVocDocumentValidatorTest::testDocumentAboutInfo()
{
- QVERIFY( 1==1 );
+ KTemporaryFile temp;
+ temp.setSuffix(".kvtml");
+ temp.open();
+ KUrl fileName = KUrl(temp.fileName());
+ temp.close();
+
+
+// KUrl fileName = KUrl("/tmp/ValidatorTestDocument.kvtml");
+
+ KEduVocDocument* doc = new KEduVocDocument;
+ doc->setAuthor("Validator Test");
+ doc->setLicense("test license");
+ doc->setDocumentComment("comment");
+ doc->setCategory("test document");
+ doc->setTitle("Validator Test Title");
+
+
+ doc->saveAs(fileName, KEduVocDocument::Kvtml, "Validator Unit Tests");
+ delete doc;
+
+ doc = new KEduVocDocument;
+ doc->open(fileName);
+
+ QVERIFY( doc->author() == "Validator Test" );
+ QVERIFY( doc->license() == "test license" );
+ QVERIFY( doc->documentComment() == "comment" );
+ QVERIFY( doc->category() == "test document" );
+ QVERIFY( doc->title() == "Validator Test Title" );
+
+
}
QTEST_KDEMAIN_CORE( KEduVocDocumentValidatorTest )