~ubuntu-branches/ubuntu/precise/gwenview/precise-proposed

« back to all changes in this revision

Viewing changes to importer/filenameformater.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2011-12-15 14:17:54 UTC
  • mto: This revision was merged to the branch mainline in revision 12.
  • Revision ID: package-import@ubuntu.com-20111215141754-z043hyx69dulbggf
Tags: upstream-4.7.90
Import upstream version 4.7.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// vim: set tabstop=4 shiftwidth=4 noexpandtab:
 
1
// vim: set tabstop=4 shiftwidth=4 expandtab:
2
2
/*
3
3
Gwenview: an image viewer
4
4
Copyright 2009 Aurélien Gâteau <agateau@kde.org>
31
31
 
32
32
// Local
33
33
 
34
 
namespace Gwenview {
 
34
namespace Gwenview
 
35
{
35
36
 
36
37
typedef QHash<QString, QString> Dict;
37
38
 
38
39
struct FileNameFormaterPrivate {
39
 
        QString mFormat;
 
40
    QString mFormat;
40
41
};
41
42
 
42
 
 
43
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
 
 
 
44
: d(new FileNameFormaterPrivate)
 
45
{
 
46
    d->mFormat = format;
 
47
}
 
48
 
 
49
FileNameFormater::~FileNameFormater()
 
50
{
 
51
    delete d;
 
52
}
 
53
 
 
54
QString FileNameFormater::format(const KUrl& url, const KDateTime& dateTime)
 
55
{
 
56
    QFileInfo info(url.fileName());
 
57
 
 
58
    // Keep in sync with helpMap()
 
59
    Dict dict;
 
60
    dict["date"]       = dateTime.toString("%Y-%m-%d");
 
61
    dict["time"]       = dateTime.toString("%H-%M-%S");
 
62
    dict["ext"]        = info.completeSuffix();
 
63
    dict["ext.lower"]  = info.completeSuffix().toLower();
 
64
    dict["name"]       = info.baseName();
 
65
    dict["name.lower"] = info.baseName().toLower();
 
66
 
 
67
    QString name;
 
68
    int length = d->mFormat.length();
 
69
    for (int pos = 0; pos < length; ++pos) {
 
70
        QChar ch = d->mFormat[pos];
 
71
        if (ch == '{') {
 
72
            if (pos == length - 1) {
 
73
                // We are at the end, ignore this
 
74
                break;
 
75
            }
 
76
            if (d->mFormat[pos + 1] == '{') {
 
77
                // This is an escaped '{', skip one
 
78
                name += '{';
 
79
                ++pos;
 
80
                continue;
 
81
            }
 
82
            int end = d->mFormat.indexOf('}', pos + 1);
 
83
            if (end == -1) {
 
84
                // No '}' found, stop here
 
85
                return name;
 
86
            }
 
87
            // Replace keyword with its value
 
88
            QString keyword = d->mFormat.mid(pos + 1, end - pos - 1);
 
89
            name += dict.value(keyword);
 
90
            pos = end;
 
91
        } else {
 
92
            name += ch;
 
93
        }
 
94
    }
 
95
    return name;
 
96
}
 
97
 
 
98
FileNameFormater::HelpMap FileNameFormater::helpMap()
 
99
{
 
100
    // Keep in sync with dict in format()
 
101
    static HelpMap map;
 
102
    if (map.isEmpty()) {
 
103
        map["date"]       = i18n("Shooting date");
 
104
        map["time"]       = i18n("Shooting time");
 
105
        map["ext"]        = i18n("Original extension");
 
106
        map["ext.lower"]  = i18n("Original extension, in lower case");
 
107
        map["name"]       = i18n("Original filename");
 
108
        map["name.lower"] = i18n("Original filename, in lower case");
 
109
    }
 
110
    return map;
 
111
}
112
112
 
113
113
} // namespace