1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2009-08-08
7  * Description : an option to provide file information to the parser
8  *
9  * Copyright (C) 2009-2012 by Andi Clemens <andi dot clemens at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "filepropertiesoption.h"
25 
26 // Qt includes
27 
28 #include <QFileInfo>
29 
30 // KDE includes
31 
32 #include <klocalizedstring.h>
33 
34 namespace
35 {
36 static const QString KEY_FILE(QLatin1String("[file]"));
37 static const QString KEY_EXT(QLatin1String("[ext]"));
38 static const QString KEY_USER(QLatin1String("[user]"));
39 static const QString KEY_GROUP(QLatin1String("[group]"));
40 }
41 
42 namespace Digikam
43 {
44 
FilePropertiesOption()45 FilePropertiesOption::FilePropertiesOption()
46     : Option(i18n("File"),
47              i18n("Add file properties"),
48              QLatin1String("folder-pictures"))
49 {
50     setUseTokenMenu(true);
51 
52     addToken(KEY_FILE, i18n("Filename"),
53              i18nc("File name", "Name"));
54 
55     addToken(KEY_EXT, i18n("File extension, prepend with a '.' character, to modify the real file extension"),
56              i18nc("File extension", "Extension"));
57 
58     addToken(KEY_USER, i18n("Owner of the file"),
59              i18nc("Owner of the file", "Owner"));
60 
61     addToken(KEY_GROUP, i18n("Group of the file"),
62              i18nc("Group of the file", "Group"));
63 
64     QString regExpStr;
65     regExpStr.append(QLatin1Char('('));
66     regExpStr.append(escapeToken(KEY_FILE)).append(QLatin1Char('|'));
67     regExpStr.append(escapeToken(KEY_USER)).append(QLatin1Char('|'));
68     regExpStr.append(escapeToken(KEY_GROUP)).append(QLatin1Char('|'));
69     regExpStr.append(QLatin1String("(\\.?)")).append(escapeToken(KEY_EXT));
70     regExpStr.append(QLatin1Char(')'));
71 
72     QRegExp reg(regExpStr);
73     reg.setMinimal(true);
74     setRegExp(reg);
75 }
76 
parseOperation(ParseSettings & settings)77 QString FilePropertiesOption::parseOperation(ParseSettings& settings)
78 {
79     QString result;
80     QFileInfo fi(settings.fileUrl.toLocalFile());
81 
82     const QRegExp& reg   = regExp();
83     const QString& token = reg.cap(1);
84 
85     if      (token == KEY_FILE)
86     {
87         result = fi.completeBaseName();
88     }
89     else if (token == KEY_USER)
90     {
91         result = fi.owner();
92     }
93     else if (token == KEY_GROUP)
94     {
95         result = fi.group();
96     }
97     else if (token == KEY_EXT)
98     {
99         result = fi.suffix();
100     }
101     else if (token == (QLatin1Char('.') + KEY_EXT))
102     {
103         result                            = QLatin1Char('.') + fi.suffix();
104         settings.useOriginalFileExtension = false;
105     }
106 
107     return result;
108 }
109 
110 } // namespace Digikam
111