File: sharedkvtmlfiles.cpp

package info (click to toggle)
libkeduvocdocument 4%3A16.08.0-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 948 kB
  • ctags: 1,086
  • sloc: cpp: 7,995; sh: 16; makefile: 10
file content (206 lines) | stat: -rw-r--r-- 6,904 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/***************************************************************************
        scan a group of KVTML documents to get information from them
    -----------------------------------------------------------------------
    copyright      : (C) 2007 Jeremy Whiting <jpwhiting@kde.org>
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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 <klocalizedstring.h>
#include <kio/copyjob.h>
#include <kio/job.h>

#include <QDir>
#include <QStandardPaths>

#include <QDebug>

class SharedKvtmlFilesPrivate
{
public:
    /** default constructor calls rescan*/
    SharedKvtmlFilesPrivate()
    {
        rescan();
    }

    /** default destructor */
    ~SharedKvtmlFilesPrivate()
    {}

    /** 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<QString, QStringList> m_filesByLang;
};

Q_GLOBAL_STATIC( SharedKvtmlFilesPrivate, sharedKvtmlFilesPrivate )

void SharedKvtmlFilesPrivate::rescan()
{
    this->m_titleList.clear();
    this->m_commentList.clear();
    this->m_filesByLang.clear();
    this->m_fileList.clear();

    QStringList locales;

    // Get all kvtml paths
    QStringList nameFilter;
    nameFilter.append("*.kvtml");
    QStringList dataPaths = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "apps/kvtml", QStandardPaths::LocateDirectory);
    Q_FOREACH (const QString &path, dataPaths) {
        qDebug() << "Checking path " << path << " for kvtml files";
        QStringList locales = QDir( path ).entryList( QDir::Dirs | QDir::NoDotAndDotDot, QDir::Name );
        Q_FOREACH (const QString &locale, locales) {
            QStringList files = QDir( path + '/' + locale ).entryList(nameFilter, QDir::Files );
            Q_FOREACH (const QString &filename, files) {
                QString filePath = path + '/' + locale + '/' + filename;
                this->m_fileList << filePath;
                this->m_filesByLang[locale].append( filePath );
            }
        }
    }

    KEduVocDocument *doc = new KEduVocDocument();
    for ( int i = 0; i < this->m_fileList.size(); ++i ) {

        // open the file
        doc->open( QUrl::fromLocalFile( 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->documentComment() );
    }
    delete doc;
}

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 );
}

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;
}

void SharedKvtmlFiles::sortDownloadedFiles()
{
    QStringList nameFilter;
    nameFilter.append("*.kvtml");
    QStringList dataPaths = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, "apps/kvtml", QStandardPaths::LocateDirectory);
    QStringList unsortedFiles;
    Q_FOREACH (const QString &path, dataPaths) {
        QStringList files = QDir( path ).entryList(nameFilter, QDir::Files | QDir::NoDotAndDotDot );
        Q_FOREACH (const QString &filename, files) {
            unsortedFiles.append( path + '/' + filename);
        }
    }

    KEduVocDocument doc;

    while ( !unsortedFiles.isEmpty() ) {
        QUrl fileUrl( QUrl::fromLocalFile( unsortedFiles.first() ) );
        // find the file's locale
        // open the file
        doc.open( fileUrl );

        if (doc.identifierCount() == 1) {
            QString locale = doc.identifier( 0 ).locale();

            // make sure the locale sub-folder exists
            QUrl pathUrl = QUrl( fileUrl );
            pathUrl = QUrl( pathUrl.toString(QUrl::RemoveFilename) + '/' + locale );
            KIO::mkdir( pathUrl );

            // move the file into the locale sub-folder
            KIO::move( fileUrl, pathUrl );
        }

        // take off the one we just did
        unsortedFiles.removeFirst();
    }

    nameFilter = QStringList(QLatin1String("*.txt"));
    QStringList khangmanFiles;
    Q_FOREACH (const QString &path, dataPaths) {
        QStringList files = QDir( path ).entryList(nameFilter, QDir::Files );
        Q_FOREACH (const QString &filename, files) {
            khangmanFiles.append( path + '/' + filename);
        }
    }

    // move khangman files into
    while ( !khangmanFiles.isEmpty() ) {
        QUrl fileUrl( QUrl::fromLocalFile( khangmanFiles.first() ) );
        QUrl destDir = QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DataLocation) + "/khangman/data/");
        // do this better with KStandardDirs stuff
        KIO::move( fileUrl, destDir);
        khangmanFiles.removeFirst();
    }

    rescan();
}