PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/0.3.1/src/lib/tools.cpp

https://gitlab.com/ScumCoder/keepassx-1
C++ | 209 lines | 168 code | 22 blank | 19 comment | 30 complexity | ec0a96ad081c8d9c36dcf5399e447cd9 MD5 | raw file
Possible License(s): GPL-2.0, AGPL-1.0
  1. /***************************************************************************
  2. * Copyright (C) 2005-2008 by Tarek Saidi *
  3. * tarek.saidi@arcor.de *
  4. * *
  5. * This program is free software; you can redistribute it and/or modify *
  6. * it under the terms of the GNU General Public License as published by *
  7. * the Free Software Foundation; version 2 of the License. *
  8. * *
  9. * This program is distributed in the hope that it will be useful, *
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  12. * GNU General Public License for more details. *
  13. * *
  14. * You should have received a copy of the GNU General Public License *
  15. * along with this program; if not, write to the *
  16. * Free Software Foundation, Inc., *
  17. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  18. ***************************************************************************/
  19. #include <QProcess>
  20. #include <QDesktopServices>
  21. void createBanner(QPixmap* Pixmap,const QPixmap* IconAlpha,const QString& Text,int Width){
  22. createBanner(Pixmap,IconAlpha,Text,Width,config->bannerColor1(),config->bannerColor2(),config->bannerTextColor());
  23. }
  24. void createBanner(QPixmap* Pixmap,const QPixmap* IconAlpha,const QString& Text,int Width, QColor Color1, QColor Color2, QColor TextColor){
  25. *Pixmap=QPixmap(Width,50);
  26. QPainter painter(Pixmap);
  27. QLinearGradient grad(0,0,Width,0);
  28. grad.setColorAt(0,Color1);
  29. grad.setColorAt(1,Color2);
  30. painter.setPen(Qt::NoPen);
  31. painter.setBrush(grad);
  32. painter.drawRect(0,0,Width,50);
  33. QPixmap Icon(32,32);
  34. if(IconAlpha){
  35. Icon.fill(TextColor);
  36. Icon.setAlphaChannel(*IconAlpha);
  37. painter.drawPixmap(10,10,Icon);
  38. }
  39. painter.setPen(QPen(TextColor));
  40. painter.setFont(QFont(QApplication::font().family(),16));
  41. painter.drawText(50,35,Text);
  42. }
  43. QString decodeFileError(QFile::FileError Code){
  44. switch(Code){
  45. case QFile::NoError: return QApplication::translate("FileErrors","No error occurred.");
  46. case QFile::ReadError: return QApplication::translate("FileErrors","An error occurred while reading from the file.");
  47. case QFile::WriteError: return QApplication::translate("FileErrors","An error occurred while writing to the file.");
  48. case QFile::FatalError: return QApplication::translate("FileErrors","A fatal error occurred.");
  49. case QFile::ResourceError: return QApplication::translate("FileErrors","An resource error occurred.");
  50. case QFile::OpenError: return QApplication::translate("FileErrors","The file could not be opened.");
  51. case QFile::AbortError: return QApplication::translate("FileErrors","The operation was aborted.");
  52. case QFile::TimeOutError: return QApplication::translate("FileErrors","A timeout occurred.");
  53. case QFile::UnspecifiedError: return QApplication::translate("FileErrors","An unspecified error occurred.");
  54. case QFile::RemoveError: return QApplication::translate("FileErrors","The file could not be removed.");
  55. case QFile::RenameError: return QApplication::translate("FileErrors","The file could not be renamed.");
  56. case QFile::PositionError: return QApplication::translate("FileErrors","The position in the file could not be changed.");
  57. case QFile::ResizeError: return QApplication::translate("FileErrors","The file could not be resized.");
  58. case QFile::PermissionsError: return QApplication::translate("FileErrors","The file could not be accessed.");
  59. case QFile::CopyError: return QApplication::translate("FileErrors","The file could not be copied.");
  60. }
  61. return QString();
  62. }
  63. void openBrowser(IEntryHandle* entry){
  64. QString url = entry->url();
  65. url.replace("{TITLE}", entry->title(), Qt::CaseInsensitive);
  66. url.replace("{USERNAME}", entry->username(), Qt::CaseInsensitive);
  67. if (url.contains("{PASSWORD}",Qt::CaseInsensitive)){
  68. SecString password=entry->password();
  69. password.unlock();
  70. url.replace("{PASSWORD}", password, Qt::CaseInsensitive);
  71. }
  72. openBrowser(url);
  73. }
  74. void openBrowser(const QString& UrlString){
  75. if (UrlString.startsWith("cmd://") && UrlString.length()>6){
  76. QProcess::startDetached(UrlString.right(UrlString.length()-6));
  77. return;
  78. }
  79. QUrl url(UrlString);
  80. if(url.scheme().isEmpty())
  81. url=QUrl("http://"+UrlString);
  82. if(config->urlCmdDef() || url.scheme()=="mailto"){
  83. QDesktopServices::openUrl(url);
  84. }
  85. else{
  86. QByteArray UrlEncoded = url.toEncoded();
  87. QString browser = config->urlCmd();
  88. if (browser.contains("%u", Qt::CaseInsensitive))
  89. browser.replace("%u", UrlEncoded, Qt::CaseInsensitive);
  90. else if (browser.contains("%1"))
  91. browser.replace("%1", UrlEncoded);
  92. else
  93. browser.append(" ").append(UrlEncoded);
  94. QProcess::startDetached(browser);
  95. }
  96. }
  97. QString makePathRelative(const QString& AbsDir,const QString& CurDir){
  98. QStringList abs=AbsDir.split('/');
  99. QStringList cur=CurDir.split('/');
  100. QString rel="./";
  101. int common;
  102. for(common=0; common < abs.size() && common < cur.size(); common++){
  103. if(abs[common]!=cur[common])break;
  104. }
  105. for(int i=0;i<cur.size()-common;i++)
  106. rel.append("../");
  107. for(int i=common;i<abs.size();i++)
  108. rel.append(abs[i]+"/");
  109. return rel;
  110. }
  111. void showErrMsg(const QString& msg,QWidget* parent){
  112. QMessageBox::critical(parent,QApplication::translate("Main","Error"),msg,QApplication::translate("Main","OK"));
  113. }
  114. QString getImageFile(const QString& name){
  115. if (QFile::exists(DataDir+"/icons/"+name))
  116. return DataDir+"/icons/"+name;
  117. else{
  118. QMessageBox::critical(0,QApplication::translate("Main","Error"),
  119. QApplication::translate("Main","File '%1' could not be found.")
  120. .arg(name),QApplication::translate("Main","OK"),0,0,2,1);
  121. exit(1);
  122. }
  123. }
  124. const QIcon& getIcon(const QString& name){
  125. static QHash<QString,QIcon*>IconCache;
  126. QIcon* CachedIcon=IconCache.value(name);
  127. if(CachedIcon)
  128. return *CachedIcon;
  129. QIcon* NewIcon=NULL;
  130. if(IconLoader){
  131. NewIcon=new QIcon(IconLoader->getIcon(name));
  132. if(NewIcon->isNull()){
  133. delete NewIcon;
  134. NewIcon=NULL;
  135. }
  136. else
  137. IconCache.insert(name,NewIcon);
  138. }
  139. if(!NewIcon)
  140. {
  141. NewIcon=new QIcon(getImageFile(name+".png"));
  142. IconCache.insert(name,NewIcon);
  143. }
  144. return *NewIcon;
  145. }
  146. const QPixmap* getPixmap(const QString& name){
  147. static QHash<QString,QPixmap*>PixmapCache;
  148. QPixmap* CachedPixmap=PixmapCache.value(name);
  149. if(CachedPixmap)
  150. return CachedPixmap;
  151. QImage img(getImageFile(name+".png"));
  152. QPixmap* NewPixmap=new QPixmap(QPixmap::fromImage(img));
  153. PixmapCache.insert(name,NewPixmap);
  154. return NewPixmap;
  155. }
  156. bool createKeyFile(const QString& filename,QString* error,int length, bool Hex){
  157. QFile file(filename);
  158. if(!file.open(QIODevice::WriteOnly|QIODevice::Truncate|QIODevice::Unbuffered)){
  159. *error=decodeFileError(file.error());
  160. return false;
  161. }
  162. if(Hex)length*=2;
  163. unsigned char* key=new unsigned char[length];
  164. randomize(key,length);
  165. if(Hex){
  166. // convert binary data to hex code (8 bit ==> 2 digits)
  167. for(int i=0; i<length; i+=2){
  168. unsigned char dig1,dig2;
  169. dig1=key[i]/16;
  170. key[i]-=(16*dig1);
  171. dig2=key[i];
  172. if(dig1>9)key[i]='A'+dig1-10;
  173. else key[i]='0'+dig1;
  174. if(dig2>9)key[i+1]='A'+dig2-10;
  175. else key[i+1]='0'+dig2;
  176. }
  177. }
  178. if(file.write((char*)key,length)==-1){
  179. delete [] key;
  180. *error=decodeFileError(file.error());
  181. file.close();
  182. return false;
  183. }
  184. file.close();
  185. delete [] key;
  186. return true;
  187. }