~ubuntu-branches/ubuntu/maverick/kdegraphics/maverick-proposed

« back to all changes in this revision

Viewing changes to gwenview/importer/filenameformater.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Thomas
  • Date: 2009-12-02 14:03:43 UTC
  • mfrom: (1.1.35 upstream)
  • Revision ID: james.westby@ubuntu.com-20091202140343-2732gbkj69g89arq
Tags: 4:4.3.80-0ubuntu1
* New upstream beta release:
  - Add build-depend on shared-desktop-ontologies for nepomuk integration
  - Bump .so versions for libkexiv, libkdcraw and libkipi
  - Update various .install files

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
2
/*
 
3
Gwenview: an image viewer
 
4
Copyright 2009 Aurélien Gâteau <agateau@kde.org>
 
5
 
 
6
This program is free software; you can redistribute it and/or
 
7
modify it under the terms of the GNU General Public License
 
8
as published by the Free Software Foundation; either version 2
 
9
of the License, or (at your option) any later version.
 
10
 
 
11
This program is distributed in the hope that it will be useful,
 
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
GNU General Public License for more details.
 
15
 
 
16
You should have received a copy of the GNU General Public License
 
17
along with this program; if not, write to the Free Software
 
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Cambridge, MA 02110-1301, USA.
 
19
 
 
20
*/
 
21
// Self
 
22
#include "filenameformater.h"
 
23
 
 
24
// Qt
 
25
#include <QFileInfo>
 
26
 
 
27
// KDE
 
28
#include <kdatetime.h>
 
29
#include <klocale.h>
 
30
#include <kurl.h>
 
31
 
 
32
// Local
 
33
 
 
34
namespace Gwenview {
 
35
 
 
36
typedef QHash<QString, QString> Dict;
 
37
 
 
38
struct FileNameFormaterPrivate {
 
39
        QString mFormat;
 
40
};
 
41
 
 
42
 
 
43
FileNameFormater::FileNameFormater(const QString& format)
 
44
: d(new FileNameFormaterPrivate) {
 
45
        d->mFormat = format;
 
46
}
 
47
 
 
48
 
 
49
FileNameFormater::~FileNameFormater() {
 
50
        delete d;
 
51
}
 
52
 
 
53
 
 
54
QString FileNameFormater::format(const KUrl& url, const KDateTime& dateTime) {
 
55
        QFileInfo info(url.fileName());
 
56
 
 
57
        // Keep in sync with helpMap()
 
58
        Dict dict;
 
59
        dict["date"]       = dateTime.toString("%Y-%m-%d");
 
60
        dict["time"]       = dateTime.toString("%H-%M-%S");
 
61
        dict["ext"]        = info.completeSuffix();
 
62
        dict["ext.lower"]  = info.completeSuffix().toLower();
 
63
        dict["name"]       = info.baseName();
 
64
        dict["name.lower"] = info.baseName().toLower();
 
65
 
 
66
        QString name;
 
67
        int length = d->mFormat.length();
 
68
        for (int pos=0; pos < length; ++pos) {
 
69
                QChar ch = d->mFormat[pos];
 
70
                if (ch == '{') {
 
71
                        if (pos == length - 1) {
 
72
                                // We are at the end, ignore this
 
73
                                break;
 
74
                        }
 
75
                        if (d->mFormat[pos + 1] == '{') {
 
76
                                // This is an escaped '{', skip one
 
77
                                name += '{';
 
78
                                ++pos;
 
79
                                continue;
 
80
                        }
 
81
                        int end = d->mFormat.indexOf('}', pos + 1);
 
82
                        if (end == -1) {
 
83
                                // No '}' found, stop here
 
84
                                return name;
 
85
                        }
 
86
                        // Replace keyword with its value
 
87
                        QString keyword = d->mFormat.mid(pos + 1, end - pos - 1);
 
88
                        name += dict.value(keyword);
 
89
                        pos = end;
 
90
                } else {
 
91
                        name += ch;
 
92
                }
 
93
        }
 
94
        return name;
 
95
}
 
96
 
 
97
 
 
98
FileNameFormater::HelpMap FileNameFormater::helpMap() {
 
99
        // Keep in sync with dict in format()
 
100
        static HelpMap map;
 
101
        if (map.isEmpty()) {
 
102
                map["date"]       = i18n("Shooting date");
 
103
                map["time"]       = i18n("Shooting time");
 
104
                map["ext"]        = i18n("Original extension");
 
105
                map["ext.lower"]  = i18n("Original extension, in lower case");
 
106
                map["name"]       = i18n("Original filename");
 
107
                map["name.lower"] = i18n("Original filename, in lower case");
 
108
        }
 
109
        return map;
 
110
}
 
111
 
 
112
 
 
113
} // namespace