From: Jeremy Paul Whiting Date: Sat, 23 Jun 2007 17:13:12 +0000 (+0000) Subject: sharedkvtmlfiles class initial checkin, kanagram is using this locally, but Im going... X-Git-Tag: v3.91.0~5 X-Git-Url: https://git.rmz.fi/?a=commitdiff_plain;h=b92989983476e1091e8017add34c8fefd226305e;p=libqmvoc.git sharedkvtmlfiles class initial checkin, kanagram is using this locally, but Im going to iron out a few issues before checking it in. svn path=/trunk/KDE/kdeedu/libkdeedu/; revision=679341 --- diff --git a/kdeeducore/CMakeLists.txt b/kdeeducore/CMakeLists.txt index 5bd5937..4dd29ed 100644 --- a/kdeeducore/CMakeLists.txt +++ b/kdeeducore/CMakeLists.txt @@ -19,6 +19,7 @@ set(kdeeducore_LIB_SRCS leitnerbox.cpp leitnersystem.cpp leitnersystemview.cpp + sharedkvtmlfiles.cpp prefleitner.cpp ) kde4_automoc(${kdeeducore_LIB_SRCS}) @@ -46,6 +47,7 @@ install(FILES leitnerbox.h leitnersystem.h leitnersystemview.h + sharedkvtmlfiles.h prefleitner.h DESTINATION ${INCLUDE_INSTALL_DIR}/libkdeedu ) diff --git a/kdeeducore/sharedkvtmlfiles.cpp b/kdeeducore/sharedkvtmlfiles.cpp new file mode 100644 index 0000000..5bbef40 --- /dev/null +++ b/kdeeducore/sharedkvtmlfiles.cpp @@ -0,0 +1,172 @@ +/*************************************************************************** + scan a group of KVTML documents to get information from them + ----------------------------------------------------------------------- + copyright : (C) 2007 Jeremy Whiting + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#include "sharedkvtmlfiles.h" + +#include "keduvocdocument.h" + +#include +#include + +#include + +#include + +class SharedKvtmlFilesPrivate +{ + public: + /** default constructor calls rescan*/ + SharedKvtmlFilesPrivate() + { + rescan(); + } + + /** default destructor */ + ~SharedKvtmlFilesPrivate() + { + } + + SharedKvtmlFiles instance; + + /** scan the folder for documents, and record what is found */ + void rescan(); + + /** list of all files found */ + QStringList m_fileList; + + /** list of all files titles found */ + QStringList m_titleList; + + /** list of file comments */ + QStringList m_commentList; + + /** map of files by language key */ + QMap m_filesByLang; +}; + +K_GLOBAL_STATIC(SharedKvtmlFilesPrivate, sharedKvtmlFilesPrivate) + +SharedKvtmlFiles *SharedKvtmlFiles::self() +{ + // returns the singleton; automatically creates a new instance if that has not happened yet. + return &sharedKvtmlFilesPrivate->instance; +} + +SharedKvtmlFiles::SharedKvtmlFiles() +{ +} + +SharedKvtmlFiles::~SharedKvtmlFiles() +{ +} + +void SharedKvtmlFilesPrivate::rescan() +{ + this->m_titleList.clear(); + this->m_commentList.clear(); + this->m_filesByLang.clear(); + this->m_fileList = KGlobal::dirs()->findAllResources("data", "kvtml/*.kvtml"); + + KEduVocDocument *doc = new KEduVocDocument(); + for (int i = 0; i < this->m_fileList.size(); ++i) + { + // open the file + doc->open(KUrl::fromPath(this->m_fileList[i])); + + // add it's title to the title list + this->m_titleList.append(doc->title()); + + // add it's comment to the comment list + this->m_commentList.append(doc->documentRemark()); + + // then add it under its main language in the map + QString language = doc->originalIdentifier(); + if (language.isEmpty()) + { + language = I18N_NOOP("none"); + } + if (this->m_filesByLang.contains(language)) + { + this->m_filesByLang[language].append(this->m_fileList[i]); + } + else + { + QStringList list; + list.append(this->m_fileList[i]); + this->m_filesByLang.insert(language, list); + } + } +} + +void SharedKvtmlFiles::rescan() +{ + sharedKvtmlFilesPrivate->rescan(); +} + +QStringList SharedKvtmlFiles::languages() +{ + return sharedKvtmlFilesPrivate->m_filesByLang.keys(); +} + +QStringList SharedKvtmlFiles::fileNames(const QString &language) +{ + // return files by language for given language if it's not empty + // otherwise return all filenames + return language.isEmpty() ? sharedKvtmlFilesPrivate->m_fileList : sharedKvtmlFilesPrivate->m_filesByLang.value(language); +} + +/** get the list of document titles found of a given language + *@param language requested language + *@return a list of document titles with words in language + */ +QStringList SharedKvtmlFiles::titles(const QString &language) +{ + QStringList retlist; + + if (language.isEmpty()) + { + retlist = sharedKvtmlFilesPrivate->m_titleList; + } + else + { + QStringList filenames = sharedKvtmlFilesPrivate->m_filesByLang.value(language); + for (int i = 0; i < filenames.size(); ++i) + { + retlist.append(sharedKvtmlFilesPrivate->m_titleList[sharedKvtmlFilesPrivate->m_fileList.indexOf(filenames[i])]); + } + } + + return retlist; +} + +QStringList SharedKvtmlFiles::comments(const QString &language) +{ + QStringList retlist; + + if (language.isEmpty()) + { + retlist = sharedKvtmlFilesPrivate->m_commentList; + } + else + { + QStringList filenames = sharedKvtmlFilesPrivate->m_filesByLang.value(language); + for (int i = 0; i < filenames.size(); ++i) + { + retlist.append(sharedKvtmlFilesPrivate->m_commentList[sharedKvtmlFilesPrivate->m_fileList.indexOf(filenames[i])]); + } + } + + return retlist; +} diff --git a/kdeeducore/sharedkvtmlfiles.h b/kdeeducore/sharedkvtmlfiles.h new file mode 100644 index 0000000..ea9682f --- /dev/null +++ b/kdeeducore/sharedkvtmlfiles.h @@ -0,0 +1,78 @@ +/*************************************************************************** + scan a group of KVTML documents to get information from them + ----------------------------------------------------------------------- + copyright : (C) 2007 Jeremy Whiting + ***************************************************************************/ + +/*************************************************************************** + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + ***************************************************************************/ + +#ifndef SHAREDKVTMLFILES_H +#define SHAREDKVTMLFILES_H + +#include "libkdeedu_core_export.h" + +#include +#include +#include + +#include + +#include "kvtmldefs.h" + +class QIODevice; +class SharedKvtmlFilesPrivate; + +/** + *namespace and singleton class to scan groups of kvtml files + *from shared kvtml location, and give information + *about files found there + *@author Jeremy Whiting + */ +class KDEEDUCORE_EXPORT SharedKvtmlFiles +{ + friend class SharedKvtmlFilesPrivate; + public: + /** singleton accessor */ + static SharedKvtmlFiles *self(); + + /** get list of all languages found in any files */ + QStringList languages(); + + /** get list of filenames found of given language + *@param language language requested QString() for all languages + *@return a list of filenames with words in language + */ + QStringList fileNames(const QString &language = QString()); + + /** get the list of document titles found of a given language + *@param language requested language QString() for all titles + *@return a list of document titles with words in language + */ + QStringList titles(const QString &language = QString()); + + /** get the list of document remarts found of a given language + *@param language requested language QString() for all comments + *@return a list of document remarks with words in language + */ + QStringList comments(const QString &language = QString()); + + /** rescan the shared kvtml locations */ + void rescan(); + + private: + /** default contstuctor */ + SharedKvtmlFiles(); + + /** default destructor */ + ~SharedKvtmlFiles(); +}; + +#endif +