Fix .desktop file path
[keepassx] / src / KpxConfig.cpp
1 /***************************************************************************
2  *   Copyright (C) 2005-2007 by Tarek Saidi                                *
3  *   tarek.saidi@arcor.de                                                  *
4  *                                                                         *
5  *   Copyright (C) 2007 by Constantin "Dinosaur" Makshin                   *
6  *   dinosaur-rus@users.sourceforge.net                                    *
7  *                                                                         *
8  *   This program is free software; you can redistribute it and/or modify  *
9  *   it under the terms of the GNU General Public License as published by  *
10  *   the Free Software Foundation; version 2 of the License.               *
11
12  *                                                                         *
13  *   This program is distributed in the hope that it will be useful,       *
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
16  *   GNU General Public License for more details.                          *
17  *                                                                         *
18  *   You should have received a copy of the GNU General Public License     *
19  *   along with this program; if not, write to the                         *
20  *   Free Software Foundation, Inc.,                                       *
21  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
22  ***************************************************************************/
23
24 #include <QLayout>
25
26 KpxConfig::KpxConfig(const QString& filePath) : settings(filePath,QSettings::IniFormat){
27         configFile=filePath;
28         if (settings.contains("Options/GroupTreeRestore") && !settings.contains("Options/GroupTreeState")){
29                 switch (settings.value("Options/GroupTreeRestore",1).toInt()){
30                         case 0:
31                                 setGroupTreeState(RestoreLast);
32                                 break;
33
34                         case 2:
35                                 setGroupTreeState(DoNothing);
36                                 break;
37
38                         default:
39                                 setGroupTreeState(ExpandAll);
40                 }
41                 settings.remove("Options/GroupTreeRestore");
42         }
43         if (urlCmd()=="<<default>>")
44                 setUrlCmd(QString());
45 }
46
47 #ifdef GLOBAL_AUTOTYPE
48 Shortcut KpxConfig::globalShortcut(){
49         Shortcut s;
50         s.key = settings.value("Options/GlobalShortcutKey",0).toUInt();
51         QBitArray mods = settings.value("Options/GlobalShortcutMods",QBitArray(5)).toBitArray();
52         if (mods.size()!=5)
53                 mods = QBitArray(5);
54         s.ctrl = mods.testBit(0);
55         s.shift = mods.testBit(1);
56         s.alt = mods.testBit(2);
57         s.altgr = mods.testBit(3);
58         s.win = mods.testBit(4);
59         
60         return s;
61 }
62
63 void KpxConfig::setGlobalShortcut(const Shortcut& s){
64         settings.setValue("Options/GlobalShortcutKey", s.key);
65         QBitArray mods(5);
66         mods.setBit(0, s.ctrl);
67         mods.setBit(1, s.shift);
68         mods.setBit(2, s.alt);
69         mods.setBit(3, s.altgr);
70         mods.setBit(4, s.win);
71         settings.setValue("Options/GlobalShortcutMods", mods);
72 }
73 #endif
74
75 unsigned KpxConfig::fileDlgHistorySize(){
76         settings.beginGroup("FileDlgHistory");
77         unsigned res=static_cast<unsigned>(settings.childKeys().size());
78         settings.endGroup();
79         return res;
80 }
81
82 QColor KpxConfig::stringToColor(const QString& str){
83         QStringList ints=str.split(',');
84         QColor res;
85         if (ints.count()>0)
86                 res.setRed(ints.at(0).toInt());
87         if (ints.count()>1)
88                 res.setGreen(ints.at(1).toInt());
89         if (ints.count()>2)
90                 res.setBlue(ints.at(2).toInt());
91         return res;
92 }
93
94 QBitArray KpxConfig::stringToBitArray(const QString& str, unsigned count){
95         QBitArray res(static_cast<int>(count));
96         if (static_cast<unsigned>(str.length())<count)
97                 count=static_cast<unsigned>(str.length());
98         for (int i=0;i<static_cast<int>(count);i++){
99                 QChar c=str.at(i);
100                 if ((c=='1') || (c=='t') || (c=='y'))
101                         res.setBit(i);
102         }
103         return res;
104 }
105
106 KpxConfig::GrpTreeState KpxConfig::stringToGrpTreeState(const QString& str){
107         GrpTreeState res=ExpandAll;
108         if (!str.compare("Restore",Qt::CaseInsensitive))
109                 res=RestoreLast;
110         else if (!str.compare("None",Qt::CaseInsensitive))
111                 res=DoNothing;
112         return res;
113 }
114
115 QList<int> KpxConfig::stringToIntArray(const QString& str, unsigned count){
116         QStringList ints=str.split(',');
117         QList<int> res;
118         unsigned i,
119                  intsCount=qMin(static_cast<unsigned>(ints.count()),count);
120         for (i=0;i<intsCount;i++)
121                 res.append(ints.at(i).toInt());
122         for (;i<count;i++)
123                 res.append(0);
124         return res;
125 }
126
127 KpxConfig::IntegrPluginType KpxConfig::stringToIntegrPluginType(const QString& str){
128         IntegrPluginType res=NoIntegr;
129         if (!str.compare("KDE",Qt::CaseInsensitive))
130                 res=KDE;
131         else if (!str.compare("Gnome",Qt::CaseInsensitive))
132                 res=Gnome;
133         return res;
134 }
135
136 tKeyType KpxConfig::stringToKeyType(const QString& str){
137         tKeyType res=PASSWORD;
138         if (!str.compare("KeyFile",Qt::CaseInsensitive))
139                 res=KEYFILE;
140         else if (!str.compare("Composite",Qt::CaseInsensitive))
141                 res=BOTH;
142         return res;
143 }
144
145 QString KpxConfig::bitArrayToString(const QBitArray& bitArray){
146         QString res;
147         for (int i=0;i<bitArray.count();i++)
148                 res.append(QString::number(bitArray.at(i)?1:0));
149         return res;
150 }
151
152 QString KpxConfig::grpTreeStateToString(GrpTreeState grpTreeState){
153         QString res;
154         switch (grpTreeState){
155                 case RestoreLast:
156                         res="Restore";
157                         break;
158
159                 case ExpandAll:
160                         res="ExpandAll";
161                         break;
162
163                 case DoNothing:
164                         res="None";
165         }
166         return res;
167 }
168
169 QString KpxConfig::intArrayToString(const QList<int>& intArray){
170         QString res;
171         if (!intArray.isEmpty())
172         {
173                 res.setNum(intArray.first());
174                 for (int i=1;i<intArray.count();i++)
175                         res.append(QString(",%1").arg(intArray.at(i)));
176         }
177         return res;
178 }
179
180 QString KpxConfig::integrPluginTypeToString(IntegrPluginType integrPluginType){
181         QString res;
182         switch (integrPluginType){
183                 case NoIntegr:
184                         res="None";
185                         break;
186
187                 case KDE:
188                         res="KDE";
189                         break;
190
191                 case Gnome:
192                         res="Gnome";
193         }
194         return res;
195 }
196
197 QString KpxConfig::keyTypeToString(tKeyType keyType){
198         QString res;
199         switch (keyType){
200                 case PASSWORD:
201                         res="Password";
202                         break;
203
204                 case KEYFILE:
205                         res="KeyFile";
206                         break;
207
208                 case BOTH:
209                         res="Composite";
210         }
211         return res;
212 }
213
214 QByteArray KpxConfig::mainWindowGeometry() {
215         QVariant var = settings.value("UI/MainWindowGeometry");
216         if (var.type() == QVariant::ByteArray)
217                 return var.toByteArray();
218         else
219                 return QByteArray();
220 }
221
222 QRect KpxConfig::dialogGeometry(const QWidget* widget){
223         Q_ASSERT(widget->parentWidget()!=NULL && widget->parentWidget()->window()!=NULL);
224         QSize size = settings.value(QString("UI/%1Size").arg(widget->objectName()),widget->size()).toSize();
225         QSize minSize = widget->minimumSize();
226         if (size.width() < minSize.width() || size.height() < minSize.height())
227                 size = minSize;
228         if (minSize.isNull() && widget->layout()!=NULL){
229                 minSize = widget->layout()->minimumSize();
230                 if (size.width() < minSize.width() || size.height() < minSize.height())
231                         size = minSize;
232         }
233
234         QRect rect;
235         rect=QRect(QPoint(), size);
236         rect.moveCenter( widget->parentWidget()->window()->geometry().center() );
237         return rect;
238 }
239
240 void KpxConfig::setDialogGeometry(const QWidget* widget){
241         settings.setValue(QString("UI/%1Size").arg(widget->objectName()),widget->size());
242 }
243
244 QString KpxConfig::detailViewTemplate(){
245         if (settings.contains("UI/DetailsView")){
246                 return QString::fromUtf8( qUncompress(settings.value("UI/DetailsView").toByteArray()) );
247         }
248         else{
249                 return defaultDetailViewTemplate();
250         }
251 }
252
253 QString KpxConfig::defaultDetailViewTemplate(){
254         QFile templ(":/default-detailview.html");
255         templ.open(QIODevice::ReadOnly);
256         QString value=QString::fromUtf8(templ.readAll());
257         templ.close();
258         value.replace("Group",QCoreApplication::translate("DetailViewTemplate","Group"));
259         value.replace("Title",QCoreApplication::translate("DetailViewTemplate","Title"));
260         value.replace("Username",QCoreApplication::translate("DetailViewTemplate","Username"));
261         value.replace("Password",QCoreApplication::translate("DetailViewTemplate","Password"));
262         value.replace("URL",QCoreApplication::translate("DetailViewTemplate","URL"));
263         value.replace("Creation",QCoreApplication::translate("DetailViewTemplate","Creation"));
264         value.replace("Last Access",QCoreApplication::translate("DetailViewTemplate","Last Access"));
265         value.replace("Last Modification",QCoreApplication::translate("DetailViewTemplate","Last Modification"));
266         value.replace("Expiration",QCoreApplication::translate("DetailViewTemplate","Expiration"));
267         value.replace("Comment",QCoreApplication::translate("DetailViewTemplate","Comment"));
268         return value;
269 }
270
271 void KpxConfig::setDetailViewTemplate(const QString& value){
272         settings.setValue("UI/DetailsView", qCompress(value.toUtf8(),9) );
273 }