~ubuntu-branches/ubuntu/raring/recorditnow/raring

« back to all changes in this revision

Viewing changes to joschy-snapshot-23-02-10/joschycore/joschycore/config.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Felix Geyer
  • Date: 2011-01-09 14:54:01 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110109145401-gyckb4airz4fio50
Tags: 0.8.1-0ubuntu1
* New upstream release. (LP: #681270)
  - Update debian/copyright.
* Build-depend on recordmydesktop.
* Add a watch file.
* Drop 01_fix_ftbfs_kwarning_call.diff, fixed upstream.
* Add 01_joschy_install_to_usr_lib.diff.
* Add 02_fix_ftbfs_no-add-needed.diff.
* Add 03_dont_install_header_files.diff.
* Replace dependency on libpolkit-qt-1-0 with policykit-1.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (C) 2010  Kai Dombrowe <just89@gmx.de>
 
3
 
 
4
    This library is free software; you can redistribute it and/or
 
5
    modify it under the terms of the GNU Lesser General Public
 
6
    License as published by the Free Software Foundation; either
 
7
    version 2.1 of the License, or (at your option) any later version.
 
8
 
 
9
    This library is distributed in the hope that it will be useful,
 
10
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
12
    Lesser General Public License for more details.
 
13
 
 
14
    You should have received a copy of the GNU Lesser General Public
 
15
    License along with this library; if not, write to the Free Software
 
16
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 
17
*/
 
18
 
 
19
 
 
20
// own
 
21
#include "config.h"
 
22
 
 
23
// Qt
 
24
#include <QtCore/QSettings>
 
25
 
 
26
 
 
27
namespace Joschy {
 
28
 
 
29
 
 
30
class ConfigPrivate : public QSharedData {
 
31
public:
 
32
    ConfigPrivate(Config *cfg)
 
33
        : q(cfg)
 
34
    {}
 
35
 
 
36
 
 
37
    static bool titleSort(const Joschy::Video &video1, const Joschy::Video &video2)
 
38
    {
 
39
        return video1.title().toLower() < video2.title().toLower();
 
40
    }
 
41
    static bool categorySort(const Joschy::Video &video1, const Joschy::Video &video2)
 
42
    {
 
43
        return video1.category().toLower() < video2.category().toLower();
 
44
    }
 
45
    static bool descriptionSort(const Joschy::Video &video1, const Joschy::Video &video2)
 
46
    {
 
47
        return video1.description().toLower() < video2.description().toLower();
 
48
    }
 
49
    static bool publishedSort(const Joschy::Video &video1, const Joschy::Video &video2)
 
50
    {
 
51
        return video1.published() < video2.published();
 
52
    }
 
53
    static bool durationSort(const Joschy::Video &video1, const Joschy::Video &video2)
 
54
    {
 
55
        return video1.duration() < video2.duration();
 
56
    }
 
57
    static bool ratingSort(const Joschy::Video &video1, const Joschy::Video &video2)
 
58
    {
 
59
        return video1.rating() < video2.rating();
 
60
    }
 
61
    static bool viewCountSort(const Joschy::Video &video1, const Joschy::Video &video2)
 
62
    {
 
63
        return video1.viewCount() < video2.viewCount();
 
64
    }
 
65
 
 
66
 
 
67
    QString group;
 
68
    QString file;
 
69
 
 
70
 
 
71
private:
 
72
    Config *q;
 
73
 
 
74
 
 
75
};
 
76
 
 
77
 
 
78
Config::Config(const QString &group, const QString &file)
 
79
    : d(new ConfigPrivate(this))
 
80
{
 
81
 
 
82
    d->group = group;
 
83
    d->file = file;
 
84
 
 
85
}
 
86
 
 
87
 
 
88
Config::Config(const Joschy::Config &copy)
 
89
    : d(copy.d)
 
90
{
 
91
 
 
92
 
 
93
}
 
94
 
 
95
 
 
96
Config::Config()
 
97
    : d(new ConfigPrivate(this))
 
98
{
 
99
 
 
100
 
 
101
}
 
102
 
 
103
 
 
104
Config::~Config()
 
105
{
 
106
 
 
107
 
 
108
 
 
109
}
 
110
 
 
111
 
 
112
Joschy::Config &Config::operator=(const Joschy::Config &rhs)
 
113
{
 
114
 
 
115
    d = rhs.d;
 
116
    return *this;
 
117
 
 
118
}
 
119
 
 
120
 
 
121
bool Config::operator==(const Joschy::Config &rhs) const
 
122
{
 
123
 
 
124
    return d == rhs.d;
 
125
 
 
126
}
 
127
 
 
128
 
 
129
bool Config::operator!=(const Joschy::Config &rhs) const
 
130
{
 
131
 
 
132
    return d != rhs.d;
 
133
 
 
134
}
 
135
 
 
136
 
 
137
bool Config::operator<(const Joschy::Config &rhs) const
 
138
{
 
139
 
 
140
    return d < rhs.d;
 
141
 
 
142
}
 
143
 
 
144
 
 
145
bool Config::operator>(const Joschy::Config &rhs) const
 
146
{
 
147
 
 
148
    return d > rhs.d;
 
149
 
 
150
}
 
151
 
 
152
 
 
153
bool Config::isValid() const
 
154
{
 
155
 
 
156
    return (!d->file.isEmpty() && !d->group.isEmpty());
 
157
 
 
158
}
 
159
 
 
160
 
 
161
QString Config::group() const
 
162
{
 
163
 
 
164
    return d->group;
 
165
 
 
166
}
 
167
 
 
168
 
 
169
QString Config::file() const
 
170
{
 
171
 
 
172
    return d->file;
 
173
 
 
174
}
 
175
 
 
176
 
 
177
QList<Joschy::Video> Config::loadVideos() const
 
178
{
 
179
 
 
180
    QList<Joschy::Video> videos;
 
181
    loadVideos(&videos);
 
182
    return videos;
 
183
 
 
184
}
 
185
 
 
186
 
 
187
void Config::setGroup(const QString &group)
 
188
{
 
189
 
 
190
    d->group = group;
 
191
 
 
192
}
 
193
 
 
194
 
 
195
void Config::setFile(const QString &file)
 
196
{
 
197
 
 
198
    d->file = file;
 
199
 
 
200
}
 
201
 
 
202
 
 
203
QString Config::createKey(const QString &k, const int &id) const
 
204
{
 
205
 
 
206
    QString key = k;
 
207
    key.append(' '+QString::number(id));
 
208
    return key;
 
209
 
 
210
}
 
211
 
 
212
 
 
213
void Config::saveVideos(const QList<Joschy::Video> &videos)
 
214
{
 
215
 
 
216
    if (!isValid()) {
 
217
        JOSCHY_DEBUG() << "Invalid cfg:" << d->file << d->group;
 
218
        return;
 
219
    }
 
220
 
 
221
    if (videos.isEmpty()) {
 
222
        return;
 
223
    }
 
224
 
 
225
    QSettings settings(d->file, QSettings::IniFormat);
 
226
    settings.beginGroup(d->group);
 
227
 
 
228
    int count = 0;
 
229
    foreach (const Joschy::Video &video, videos) {
 
230
        foreach (const QString &propertyKey, video.propertys()) {
 
231
            settings.setValue(createKey(propertyKey, count), video.property(propertyKey));
 
232
        }
 
233
        settings.setValue(createKey("PropertyKeys", count), video.propertys());
 
234
        count++;
 
235
    }
 
236
    settings.setValue("VideoCount", count);
 
237
 
 
238
    settings.endGroup();
 
239
    settings.sync();
 
240
 
 
241
}
 
242
 
 
243
 
 
244
void Config::loadVideos(QList<Joschy::Video> *videos) const
 
245
{
 
246
 
 
247
    if (!isValid()) {
 
248
        JOSCHY_DEBUG() << "Invalid cfg:" << d->file << d->group;
 
249
        return;
 
250
    }
 
251
 
 
252
    QSettings settings(d->file, QSettings::IniFormat);
 
253
    settings.beginGroup(d->group);
 
254
 
 
255
    const int count = settings.value("VideoCount", 0).toInt();
 
256
    for (int i = 0; i < count; i++) {
 
257
        Joschy::Video video;
 
258
        const QStringList keys = settings.value(createKey("PropertyKeys", i), QStringList()).toStringList();
 
259
        foreach (const QString &propertyKey, keys) {
 
260
            video.setProperty(propertyKey, settings.value(createKey(propertyKey, i)));
 
261
        }
 
262
        videos->append(video);
 
263
    }
 
264
 
 
265
    settings.endGroup();
 
266
 
 
267
}
 
268
 
 
269
 
 
270
QList<Joschy::Video> Config::sortVideos(const QList<Joschy::Video> &videos,
 
271
                                        const Joschy::Config::SortType &type)
 
272
{
 
273
 
 
274
    QList<Joschy::Video> list = videos;
 
275
    sortVideos(&list, type);
 
276
    return list;
 
277
 
 
278
}
 
279
 
 
280
 
 
281
void Config::sortVideos(QList<Joschy::Video> *videos, const Joschy::Config::SortType &type)
 
282
{
 
283
 
 
284
    switch (type) {
 
285
    case Config::SortByTitleType: qStableSort(videos->begin(), videos->end(), ConfigPrivate::titleSort); break;
 
286
    case Config::SortByDescriptionType: qStableSort(videos->begin(), videos->end(), ConfigPrivate::descriptionSort); break;
 
287
    case Config::SortByPublishedType: qStableSort(videos->begin(), videos->end(), ConfigPrivate::publishedSort); break;
 
288
    case Config::SortByDurationType: qStableSort(videos->begin(), videos->end(), ConfigPrivate::durationSort); break;
 
289
    case Config::SortByRatingType: qStableSort(videos->begin(), videos->end(), ConfigPrivate::ratingSort); break;
 
290
    case Config::SortByViewCountType: qStableSort(videos->begin(), videos->end(), ConfigPrivate::viewCountSort); break;
 
291
    case Config::SortByCategoryType: qStableSort(videos->begin(), videos->end(), ConfigPrivate::categorySort); break;
 
292
    }
 
293
 
 
294
}
 
295
 
 
296
 
 
297
 
 
298
 
 
299
} // namespace Joschy
 
300