/*************************************************************************** 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 #include class SharedKvtmlFilesPrivate { public: SharedKvtmlFiles instance; /** 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() { rescan(); } SharedKvtmlFiles::~SharedKvtmlFiles() { } void SharedKvtmlFiles::rescan() { sharedKvtmlFilesPrivate->m_titleList.clear(); sharedKvtmlFilesPrivate->m_commentList.clear(); sharedKvtmlFilesPrivate->m_filesByLang.clear(); sharedKvtmlFilesPrivate->m_fileList = KGlobal::dirs()->findAllResources("data", "kvtml/*.kvtml"); KEduVocDocument *doc = new KEduVocDocument(); for (int i = 0; i < sharedKvtmlFilesPrivate->m_fileList.size(); ++i) { // open the file doc->open(KUrl::fromPath(sharedKvtmlFilesPrivate->m_fileList[i])); // add it's title to the title list sharedKvtmlFilesPrivate->m_titleList.append(doc->title()); // add it's comment to the comment list sharedKvtmlFilesPrivate->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 (sharedKvtmlFilesPrivate->m_filesByLang.contains(language)) { sharedKvtmlFilesPrivate->m_filesByLang[language].append(sharedKvtmlFilesPrivate->m_fileList[i]); } else { QStringList list; list.append(sharedKvtmlFilesPrivate->m_fileList[i]); sharedKvtmlFilesPrivate->m_filesByLang.insert(language, list); } } } 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; }