• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

libkdeedu/keduvocdocument

  • sources
  • kde-4.12
  • kdeedu
  • libkdeedu
  • keduvocdocument
sharedkvtmlfiles.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  scan a group of KVTML documents to get information from them
3  -----------------------------------------------------------------------
4  copyright : (C) 2007 Jeremy Whiting <jpwhiting@kde.org>
5  ***************************************************************************/
6 
7 /***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 
16 #include "sharedkvtmlfiles.h"
17 
18 #include "keduvocdocument.h"
19 
20 #include <kio/copyjob.h>
21 #include <kio/job.h>
22 #include <klocale.h>
23 #include <kstandarddirs.h>
24 
25 #include <QDir>
26 #include <QList>
27 #include <QSet>
28 
29 #include <kglobal.h>
30 
31 class SharedKvtmlFilesPrivate
32 {
33 public:
35  SharedKvtmlFilesPrivate()
36  {
37  rescan();
38  }
39 
41  ~SharedKvtmlFilesPrivate()
42  {}
43 
45  void rescan();
46 
48  QStringList m_fileList;
49 
51  QStringList m_titleList;
52 
54  QStringList m_commentList;
55 
57  QMap<QString, QStringList> m_filesByLang;
58 };
59 
60 K_GLOBAL_STATIC( SharedKvtmlFilesPrivate, sharedKvtmlFilesPrivate )
61 
62 void SharedKvtmlFilesPrivate::rescan()
63 {
64  this->m_titleList.clear();
65  this->m_commentList.clear();
66  this->m_filesByLang.clear();
67  this->m_fileList.clear();
68 
69  QStringList locales;
70 
71  QStringList dataPaths = KGlobal::dirs()->findDirs( "data", "kvtml/" );
72  for ( int i = 0; i < dataPaths.size(); ++i ) {
73  locales += QDir( dataPaths[i] ).entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name );
74  }
75 
76  // remove duplicates
77  locales = locales.toSet().toList();
78 
79  for ( int i = 0; i < locales.size(); ++i ) {
80  // get all files for this language
81  QStringList thisLangFiles = KGlobal::dirs()->findAllResources( "data",
82  QString( "kvtml/%1/*.kvtml" ).arg( QDir( locales[i] ).dirName() ) );
83  // add them to the big list
84  this->m_fileList << thisLangFiles;
85 
86  // then add them to their respective language maps
87  for ( int j = 0; j < thisLangFiles.size(); ++j ) {
88  this->m_filesByLang[locales[i]].append( thisLangFiles[j] );
89  }
90  }
91 
92  KEduVocDocument *doc = new KEduVocDocument();
93  for ( int i = 0; i < this->m_fileList.size(); ++i ) {
94 
95  // open the file
96  doc->open( KUrl::fromPath( this->m_fileList[i] ) );
97 
98  // add it's title to the title list
99  this->m_titleList.append( doc->title() );
100 
101  // add it's comment to the comment list
102  this->m_commentList.append( doc->documentComment() );
103  }
104  delete doc;
105 }
106 
107 void SharedKvtmlFiles::rescan()
108 {
109  sharedKvtmlFilesPrivate->rescan();
110 }
111 
112 QStringList SharedKvtmlFiles::languages()
113 {
114  return sharedKvtmlFilesPrivate->m_filesByLang.keys();
115 }
116 
117 QStringList SharedKvtmlFiles::fileNames( const QString &language )
118 {
119  // return files by language for given language if it's not empty
120  // otherwise return all filenames
121  return language.isEmpty() ? sharedKvtmlFilesPrivate->m_fileList : sharedKvtmlFilesPrivate->m_filesByLang.value( language );
122 }
123 
124 QStringList SharedKvtmlFiles::titles( const QString &language )
125 {
126  QStringList retlist;
127 
128  if ( language.isEmpty() ) {
129  retlist = sharedKvtmlFilesPrivate->m_titleList;
130  } else {
131  QStringList filenames = sharedKvtmlFilesPrivate->m_filesByLang.value( language );
132  for ( int i = 0; i < filenames.size(); ++i ) {
133  retlist.append( sharedKvtmlFilesPrivate->m_titleList[sharedKvtmlFilesPrivate->m_fileList.indexOf( filenames[i] )] );
134  }
135  }
136 
137  return retlist;
138 }
139 
140 QStringList SharedKvtmlFiles::comments( const QString &language )
141 {
142  QStringList retlist;
143 
144  if ( language.isEmpty() ) {
145  retlist = sharedKvtmlFilesPrivate->m_commentList;
146  } else {
147  QStringList filenames = sharedKvtmlFilesPrivate->m_filesByLang.value( language );
148  for ( int i = 0; i < filenames.size(); ++i ) {
149  retlist.append( sharedKvtmlFilesPrivate->m_commentList[sharedKvtmlFilesPrivate->m_fileList.indexOf( filenames[i] )] );
150  }
151  }
152 
153  return retlist;
154 }
155 
156 void SharedKvtmlFiles::sortDownloadedFiles()
157 {
158  QStringList unsortedFiles = KGlobal::dirs()->findAllResources( "data",
159  QString( "kvtml/*.kvtml" ) );
160 
161  KEduVocDocument doc;
162 
163  while ( !unsortedFiles.isEmpty() ) {
164  KUrl fileUrl( KUrl::fromPath( unsortedFiles.first() ) );
165  // find the file's locale
166  // open the file
167  doc.open( fileUrl );
168 
169  if (doc.identifierCount() == 1) {
170  QString locale = doc.identifier( 0 ).locale();
171 
172  // make sure the locale sub-folder exists
173  KUrl pathUrl( fileUrl );
174  pathUrl.setFileName( QString() );
175  pathUrl.addPath( locale );
176  KIO::mkdir( pathUrl );
177 
178  // move the file into the locale sub-folder
179  KIO::move( fileUrl, pathUrl );
180  }
181 
182  // take off the one we just did
183  unsortedFiles.removeFirst();
184  }
185 
186  QStringList khangmanFiles = KGlobal::dirs()->findAllResources( "data",
187  QString( "kvtml/*.txt" ) );
188 
189  // move khangman files into
190  while ( !khangmanFiles.isEmpty() ) {
191  KUrl fileUrl( KUrl::fromPath( khangmanFiles.first() ) );
192  KUrl destDir = KUrl::fromPath(KStandardDirs::locateLocal("appdata", "khangman/data/"));
193  // do this better with KStandardDirs stuff
194  KIO::move( fileUrl, destDir);
195  khangmanFiles.removeFirst();
196  }
197 
198  rescan();
199 }
keduvocdocument.h
SharedKvtmlFiles::languages
KEDUVOCDOCUMENT_EXPORT QStringList languages()
get list of all languages found in any files
Definition: sharedkvtmlfiles.cpp:112
sharedkvtmlfiles.h
SharedKvtmlFiles::rescan
KEDUVOCDOCUMENT_EXPORT void rescan()
rescan the shared kvtml locations
Definition: sharedkvtmlfiles.cpp:107
KEduVocDocument::open
int open(const KUrl &url)
Open a document file.
Definition: keduvocdocument.cpp:239
KEduVocDocument::title
QString title() const
Definition: keduvocdocument.cpp:741
SharedKvtmlFiles::comments
KEDUVOCDOCUMENT_EXPORT QStringList comments(const QString &language=QString())
get the list of document remarks found of a given language
Definition: sharedkvtmlfiles.cpp:140
SharedKvtmlFiles::sortDownloadedFiles
KEDUVOCDOCUMENT_EXPORT void sortDownloadedFiles()
sort files downloaded to kvtml top-level dir into locale sub-folders
Definition: sharedkvtmlfiles.cpp:156
KEduVocDocument::documentComment
QString documentComment() const
Definition: keduvocdocument.cpp:783
KEduVocDocument::identifier
KEduVocIdentifier & identifier(int index)
Returns the identifier of translation index.
Definition: keduvocdocument.cpp:654
KEduVocIdentifier::locale
QString locale() const
The locale of the contents: en, de, es, ...
Definition: keduvocidentifier.cpp:91
SharedKvtmlFiles::titles
KEDUVOCDOCUMENT_EXPORT QStringList titles(const QString &language=QString())
get the list of document titles found of a given language
Definition: sharedkvtmlfiles.cpp:124
KEduVocDocument::identifierCount
int identifierCount() const
Definition: keduvocdocument.cpp:694
SharedKvtmlFiles::fileNames
KEDUVOCDOCUMENT_EXPORT QStringList fileNames(const QString &language=QString())
get list of filenames found of given language
Definition: sharedkvtmlfiles.cpp:117
KEduVocDocument
This class contains the expressions of your vocabulary as well as other information about the vocabul...
Definition: keduvocdocument.h:44
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:37:22 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

libkdeedu/keduvocdocument

Skip menu "libkdeedu/keduvocdocument"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal