1 /*********
2 *
3 * This file is part of BibleTime's source code, http://www.bibletime.info/.
4 *
5 * Copyright 1999-2016 by the BibleTime developers.
6 * The BibleTime source code is licensed under the GNU General Public License version 2.0.
7 *
8 **********/
9 
10 #include "tool.h"
11 
12 #include <QApplication>
13 #include <QFile>
14 #include <QFileDialog>
15 #include <QLabel>
16 #include <QRegExp>
17 #include <QTextStream>
18 #include <QWidget>
19 #include "../backend/drivers/cswordmoduleinfo.h"
20 #include "../backend/managers/cswordbackend.h"
21 #include "../bibletimeapp.h"
22 #include "../frontend/messagedialog.h"
23 #include "btassert.h"
24 #include "cresmgr.h"
25 #include "directory.h"
26 
27 
28 namespace util {
29 namespace tool {
30 
31 
32 /** Creates the file filename and put text into the file.
33  */
savePlainFile(const QString & filename,const QString & text,const bool forceOverwrite,QTextCodec * const fileCodec)34 bool savePlainFile(const QString & filename,
35                    const QString & text,
36                    const bool forceOverwrite,
37                    QTextCodec * const fileCodec)
38 {
39     BT_ASSERT(fileCodec);
40     BT_ASSERT(!filename.isEmpty());
41 
42     QFile saveFile(filename);
43 
44     if (saveFile.exists()) {
45         if (!forceOverwrite
46             && message::showQuestion(nullptr, QObject::tr("Overwrite File?"),
47                 QString::fromLatin1("<qt><b>%1</b><br/>%2</qt>")
48                 .arg( QObject::tr("The file already exists.") )
49                 .arg( QObject::tr("Do you want to overwrite it?")),
50                 QMessageBox::Yes | QMessageBox::No,
51                 QMessageBox::No) == QMessageBox::No)
52         {
53             return false;
54         }
55         else { //either the user chose yes or forceOverwrite is set
56             saveFile.remove();
57         }
58     }
59 
60     if (saveFile.open(QIODevice::ReadWrite)) {
61         QTextStream textstream(&saveFile);
62         textstream.setCodec(fileCodec);
63         textstream << text;
64         textstream.flush();
65         saveFile.close();
66         if (saveFile.error() == QFile::NoError)
67             return true;
68 
69         QMessageBox::critical(nullptr, QObject::tr("Error"),
70                               QString::fromLatin1("<qt>%1<br/><b>%2</b></qt>")
71                                   .arg(QObject::tr("Error while writing to file."))
72                                   .arg(QObject::tr("Please check that enough disk space is available.")));
73     }
74     else {
75         QMessageBox::critical(nullptr, QObject::tr("Error"),
76                               QString::fromLatin1("<qt>%1<br/><b>%2</b></qt>")
77                                   .arg(QObject::tr("The file couldn't be opened for saving."))
78                                   .arg(QObject::tr("Please check permissions etc.")));
79     }
80 
81     return false;
82 }
83 
getIconForModule(const CSwordModuleInfo * const module)84 QIcon const & getIconForModule(const CSwordModuleInfo * const module) {
85     if (!module)
86         return CResMgr::modules::book::icon_locked();
87 
88     if (module->category() == CSwordModuleInfo::Cult)
89         return CResMgr::modules::icon_cult();
90 
91     switch (module->type()) {
92         case CSwordModuleInfo::Bible:
93             if (module->isLocked())
94                 return CResMgr::modules::bible::icon_locked();
95             return CResMgr::modules::bible::icon_unlocked();
96 
97         case CSwordModuleInfo::Lexicon:
98             if (module->isLocked())
99                 return CResMgr::modules::lexicon::icon_locked();
100             return CResMgr::modules::lexicon::icon_unlocked();
101 
102         case CSwordModuleInfo::Commentary:
103             if (module->isLocked())
104                 return CResMgr::modules::commentary::icon_locked();
105             return CResMgr::modules::commentary::icon_unlocked();
106 
107         case CSwordModuleInfo::GenericBook:
108             if (module->isLocked())
109                 return CResMgr::modules::book::icon_locked();
110             return CResMgr::modules::book::icon_unlocked();
111 
112         case CSwordModuleInfo::Unknown: //fallback
113         default:
114             if (module->isLocked())
115                 return CResMgr::modules::book::icon_locked();
116             return CResMgr::modules::book::icon_unlocked();
117     }
118     return CResMgr::modules::book::icon_unlocked();
119 }
120 
explanationLabel(QWidget * const parent,const QString & heading,const QString & text)121 QLabel * explanationLabel(QWidget * const parent,
122                           const QString & heading,
123                           const QString & text)
124 {
125     QLabel * const label = new QLabel(parent);
126     initExplanationLabel(label, heading, text);
127     return label;
128 }
129 
initExplanationLabel(QLabel * const label,const QString & heading,const QString & text)130 void initExplanationLabel(QLabel * const label,
131                           const QString & heading,
132                           const QString & text)
133 {
134     QString labelText;
135     if (!heading.isEmpty()) {
136         labelText += "<b>";
137         labelText += heading;
138         labelText += "</b>";
139     }
140     if (!heading.isEmpty() && !text.isEmpty()) {
141         labelText += "<span style=\"white-space:pre\">  -  </span>";
142     }
143     if (!text.isEmpty()) {
144         labelText += "<small>";
145         labelText += text;
146         labelText += "</small>";
147     }
148     label->setText(labelText);
149     label->setWordWrap(true);
150     label->setMargin(1);
151     label->setFrameStyle(QFrame::Box | QFrame::Sunken);
152 }
153 
inHTMLTag(const int pos,const QString & text)154 bool inHTMLTag(const int pos, const QString & text) {
155     int i1 = text.lastIndexOf("<", pos);
156     int i2 = text.lastIndexOf(">", pos);
157     int i3 = text.indexOf(">", pos);
158     int i4 = text.indexOf("<", pos);
159 
160 
161     // if ((i1>0) && (i2==-1))  //we're in th first html tag
162     //  i2=i1; // not ncessary, just for explanation
163 
164     if ((i3 > 0) && (i4 == -1))  //we're in the last html tag
165         i4 = i3 + 1;
166 
167     //  qWarning("%d > %d && %d < %d",i1,i2,i3,i4);
168 
169     return (i1 > i2) && (i3 < i4);
170 }
171 
remoteModuleToolTip(const CSwordModuleInfo & module,const QString & localVer)172 QString remoteModuleToolTip(const CSwordModuleInfo & module,
173                             const QString & localVer)
174 {
175     QString text = "<p style='white-space:pre'><b>";
176     text += module.name();
177     text += "</b> ";
178 
179     if (module.category() == CSwordModuleInfo::Cult) {
180         text += "<small><b>";
181         text += QObject::tr("Take care, this work contains cult / questionable "
182                             "material!");
183         text += "</b></small><br/>";
184     }
185 
186     text += "<small>(";
187     text += module.config(CSwordModuleInfo::Description);
188     text += ")</small><hr/>";
189 
190     if (module.isEncrypted()) {
191         text += QObject::tr("Encrypted - needs unlock key");
192         text += "<br/>";
193     }
194 
195     if (!localVer.isEmpty()) {
196         text += "<b>";
197         text += QObject::tr("Updated version available!");
198         text += "</b><br/>";
199     }
200 
201     if (module.hasVersion()) {
202         text += QObject::tr("Version");
203         text += ": ";
204         text += module.config(CSwordModuleInfo::ModuleVersion);
205     }
206 
207     // if installed already
208     if (!localVer.isEmpty()) {
209         text += "  ";
210         text += QObject::tr("Installed version");
211         text += ": ";
212         text += localVer;
213     }
214     text += "<br/><small>(";
215     text += QObject::tr("Double click for more information");
216     text += ")</small></p>";
217 
218     return text;
219 }
220 
221 
mWidth(const QWidget * const widget,const int mCount)222 int mWidth(const QWidget * const widget, const int mCount) {
223     const QString mString(mCount, 'M');
224     if (widget)
225         return widget->fontMetrics().width(mString);
226     return QApplication::fontMetrics().width(mString);
227 }
228 
229 
230 } // namespace tool {
231 } // namespace util {
232