PageRenderTime 22ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/common/Common.cpp

https://bitbucket.org/sealibora/xtee-connector
C++ | 319 lines | 262 code | 23 blank | 34 comment | 37 complexity | 42ce4d504d55a06ded1c5ed28a658697 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-3.0
  1. /*
  2. * QEstEidCommon
  3. *
  4. * Copyright (C) 2009 Jargo K覺ster <jargo@innovaatik.ee>
  5. * Copyright (C) 2009 Raul Metsma <raul@innovaatik.ee>
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. */
  22. #include "Common.h"
  23. #include <QDateTime>
  24. #include <QDesktopServices>
  25. #include <QFileInfo>
  26. #include <QProcess>
  27. #include <QTextStream>
  28. #include <QUrl>
  29. #ifdef Q_OS_WIN32
  30. #include <QDir>
  31. #include <QLibrary>
  32. #include <QTemporaryFile>
  33. #include <windows.h>
  34. #include <mapi.h>
  35. #endif
  36. #ifdef Q_OS_MAC
  37. #include <Carbon/Carbon.h>
  38. #endif
  39. #include "SslCertificate.h"
  40. Common::Common( QObject *parent ): QObject( parent ) {}
  41. bool Common::canWrite( const QString &filename )
  42. {
  43. #ifdef Q_OS_WIN32
  44. return QTemporaryFile( QFileInfo( filename ).absolutePath().append( "/XXXXXX" ) ).open();
  45. #else
  46. QFileInfo file( filename );
  47. return file.isFile() ? file.isWritable() : QFileInfo( file.absolutePath() ).isWritable();
  48. #endif
  49. }
  50. void Common::browse( const QUrl &url )
  51. {
  52. QUrl u = url;
  53. u.setScheme( "file" );
  54. bool started = false;
  55. #if defined(Q_OS_WIN32)
  56. started = QProcess::startDetached( "explorer", QStringList() << "/select," <<
  57. QDir::toNativeSeparators( u.toLocalFile() ) );
  58. #elif defined(Q_OS_MAC)
  59. started = QProcess::startDetached("/usr/bin/osascript", QStringList() <<
  60. "-e" << "on run argv" <<
  61. "-e" << "set vfile to POSIX file (item 1 of argv)" <<
  62. "-e" << "tell application \"Finder\"" <<
  63. "-e" << "select vfile" <<
  64. "-e" << "activate" <<
  65. "-e" << "end tell" <<
  66. "-e" << "end run" <<
  67. // Commandline arguments
  68. u.toLocalFile());
  69. #endif
  70. if( started )
  71. return;
  72. QDesktopServices::openUrl( QUrl::fromLocalFile( QFileInfo( u.toLocalFile() ).absolutePath() ) );
  73. }
  74. QString Common::fileSize( quint64 bytes )
  75. {
  76. const quint64 kb = 1024;
  77. const quint64 mb = 1024 * kb;
  78. const quint64 gb = 1024 * mb;
  79. const quint64 tb = 1024 * gb;
  80. if( bytes >= tb )
  81. return QString( "%1 TB" ).arg( qreal(bytes) / tb, 0, 'f', 3 );
  82. if( bytes >= gb )
  83. return QString( "%1 GB" ).arg( qreal(bytes) / gb, 0, 'f', 2 );
  84. if( bytes >= mb )
  85. return QString( "%1 MB" ).arg( qreal(bytes) / mb, 0, 'f', 1 );
  86. if( bytes >= kb )
  87. return QString( "%1 KB" ).arg( bytes / kb );
  88. return QString( "%1 B" ).arg( bytes );
  89. }
  90. void Common::mailTo( const QUrl &url )
  91. {
  92. #if defined(Q_OS_WIN32)
  93. QString file = url.queryItemValue( "attachment" );
  94. QByteArray filePath = QDir::toNativeSeparators( file ).toLatin1();
  95. QByteArray fileName = QFileInfo( file ).fileName().toLatin1();
  96. QByteArray subject = url.queryItemValue( "subject" ).toLatin1();
  97. MapiFileDesc doc[1];
  98. doc[0].ulReserved = 0;
  99. doc[0].flFlags = 0;
  100. doc[0].nPosition = -1;
  101. doc[0].lpszPathName = const_cast<char*>(filePath.constData());
  102. doc[0].lpszFileName = const_cast<char*>(fileName.constData());
  103. doc[0].lpFileType = NULL;
  104. // Create message
  105. MapiMessage message;
  106. message.ulReserved = 0;
  107. message.lpszSubject = const_cast<char*>(subject.constData());
  108. message.lpszNoteText = "";
  109. message.lpszMessageType = NULL;
  110. message.lpszDateReceived = NULL;
  111. message.lpszConversationID = NULL;
  112. message.flFlags = 0;
  113. message.lpOriginator = NULL;
  114. message.nRecipCount = 0;
  115. message.lpRecips = NULL;
  116. message.nFileCount = 1;
  117. message.lpFiles = (lpMapiFileDesc)&doc;
  118. QLibrary lib("mapi32");
  119. typedef ULONG (PASCAL *SendMail)(ULONG,ULONG,MapiMessage*,FLAGS,ULONG);
  120. SendMail mapi = (SendMail)lib.resolve("MAPISendMail");
  121. if( mapi )
  122. {
  123. mapi( NULL, 0, &message, MAPI_LOGON_UI|MAPI_DIALOG, 0 );
  124. return;
  125. }
  126. #elif defined(Q_OS_MAC)
  127. CFURLRef emailUrl = CFURLCreateWithString(kCFAllocatorDefault, CFSTR("mailto:info@example.com"), NULL), appUrl = NULL;
  128. bool started = false;
  129. if(LSGetApplicationForURL(emailUrl, kLSRolesEditor, NULL, &appUrl) == noErr)
  130. {
  131. CFStringRef appPath = CFURLCopyFileSystemPath(appUrl, kCFURLPOSIXPathStyle);
  132. if(appPath != NULL)
  133. {
  134. if(CFStringCompare(appPath, CFSTR("/Applications/Mail.app"), 0) == kCFCompareEqualTo)
  135. {
  136. started = QProcess::startDetached("/usr/bin/osascript", QStringList() <<
  137. "-e" << "on run argv" <<
  138. "-e" << "set vattachment to (item 1 of argv)" <<
  139. "-e" << "set vsubject to (item 2 of argv)" <<
  140. "-e" << "tell application \"Mail\"" <<
  141. "-e" << "set composeMessage to make new outgoing message at beginning with properties {visible:true}" <<
  142. "-e" << "tell composeMessage" <<
  143. "-e" << "set subject to vsubject" <<
  144. "-e" << "set content to \" \"" <<
  145. "-e" << "tell content" <<
  146. "-e" << "make new attachment with properties {file name: vattachment} at after the last word of the last paragraph" <<
  147. "-e" << "end tell" <<
  148. "-e" << "end tell" <<
  149. "-e" << "activate" <<
  150. "-e" << "end tell" <<
  151. "-e" << "end run" <<
  152. // Commandline arguments
  153. url.queryItemValue("attachment") <<
  154. url.queryItemValue("subject"));
  155. }
  156. else if(CFStringFind(appPath, CFSTR("Entourage"), 0).location != kCFNotFound)
  157. {
  158. started = QProcess::startDetached("/usr/bin/osascript", QStringList() <<
  159. "-e" << "on run argv" <<
  160. "-e" << "set vattachment to (item 1 of argv)" <<
  161. "-e" << "set vsubject to (item 2 of argv)" <<
  162. "-e" << "tell application \"Microsoft Entourage\"" <<
  163. "-e" << "set vmessage to make new outgoing message with properties" <<
  164. "-e" << "{subject:vsubject, attachments:vattachment}" <<
  165. "-e" << "open vmessage" <<
  166. "-e" << "activate" <<
  167. "-e" << "end tell" <<
  168. "-e" << "end run" <<
  169. // Commandline arguments
  170. url.queryItemValue("attachment") <<
  171. url.queryItemValue("subject"));
  172. }
  173. else if(CFStringCompare(appPath, CFSTR("/Applications/Thunderbird.app"), 0) == kCFCompareEqualTo)
  174. {
  175. // TODO: Handle Thunderbird here? Impossible?
  176. }
  177. CFRelease(appPath);
  178. }
  179. CFRelease(appUrl);
  180. }
  181. CFRelease(emailUrl);
  182. if( started )
  183. return;
  184. #elif defined(Q_OS_LINUX)
  185. QByteArray thunderbird;
  186. QProcess p;
  187. QStringList env = QProcess::systemEnvironment();
  188. if( env.indexOf( QRegExp("KDE_FULL_SESSION.*") ) != -1 )
  189. {
  190. p.start( "kreadconfig", QStringList()
  191. << "--file" << "emaildefaults"
  192. << "--group" << "PROFILE_Default"
  193. << "--key" << "EmailClient" );
  194. p.waitForFinished();
  195. QByteArray data = p.readAllStandardOutput().trimmed();
  196. if( data.contains("thunderbird") )
  197. thunderbird = data;
  198. }
  199. else if( env.indexOf( QRegExp("GNOME_DESKTOP_SESSION_ID.*") ) != -1 )
  200. {
  201. p.start( "gconftool-2", QStringList()
  202. << "--get" << "/desktop/gnome/url-handlers/mailto/command" );
  203. p.waitForFinished();
  204. QByteArray data = p.readAllStandardOutput();
  205. if( data.contains("thunderbird") )
  206. thunderbird = data.split(' ').value(0);
  207. }
  208. /*
  209. else
  210. {
  211. p.start( "xprop", QStringList() << "-root" << "_DT_SAVE_MODE" );
  212. p.waitForFinished();
  213. if( p.readAllStandardOutput().contains("xfce4") )
  214. {}
  215. }*/
  216. if( !thunderbird.isEmpty() )
  217. {
  218. if( p.startDetached( thunderbird, QStringList() << "-compose"
  219. << QString( "subject='%1',attachment='%2,'" )
  220. .arg( url.queryItemValue( "subject" ) )
  221. .arg( QUrl::fromLocalFile( url.queryItemValue( "attachment" ) ).toString() ) ) );
  222. return;
  223. }
  224. else
  225. {
  226. if( p.startDetached( "xdg-email", QStringList()
  227. << "--subject" << url.queryItemValue( "subject" )
  228. << "--attach" << url.queryItemValue( "attachment" ) ) )
  229. return;
  230. }
  231. #endif
  232. QDesktopServices::openUrl( url );
  233. }
  234. void Common::showHelp( const QString &msg )
  235. {
  236. QUrl u( "http://support.sk.ee/" );
  237. u.addQueryItem( "searchquery", msg );
  238. u.addQueryItem( "searchtype", "all" );
  239. u.addQueryItem( "_m", "core" );
  240. u.addQueryItem( "_a", "searchclient" );
  241. QDesktopServices::openUrl( u );
  242. }
  243. bool Common::startDetached( const QString &program )
  244. { return startDetached( program, QStringList() ); }
  245. bool Common::startDetached( const QString &program, const QStringList &arguments )
  246. {
  247. #ifdef Q_OS_MAC
  248. return QProcess::startDetached( "/usr/bin/open", QStringList() << "-a" << program << arguments );
  249. #else
  250. return QProcess::startDetached( program, arguments );
  251. #endif
  252. }
  253. QString Common::tokenInfo( CertType type, const QString &card, const QSslCertificate &cert )
  254. {
  255. QString content;
  256. QTextStream s( &content );
  257. SslCertificate c( cert );
  258. s << "<table width=\"100%\"><tr><td>";
  259. if( c.isTempel() )
  260. {
  261. s << tr("Company") << ": <font color=\"black\">"
  262. << c.toString( "CN" ) << "</font><br />";
  263. s << tr("Register code") << ": <font color=\"black\">"
  264. << c.subjectInfo( "serialNumber" ) << "</font><br />";
  265. }
  266. else
  267. {
  268. s << tr("Name") << ": <font color=\"black\">"
  269. << c.toString( "GN SN" ) << "</font><br />";
  270. s << tr("Personal code") << ": <font color=\"black\">"
  271. << c.subjectInfo( "serialNumber" ) << "</font><br />";
  272. }
  273. s << tr("Card in reader") << ": <font color=\"black\">" << card << "</font><br />";
  274. bool willExpire = c.expiryDate().toLocalTime() <= QDateTime::currentDateTime().addDays( 105 );
  275. s << (type == AuthCert ? tr("Auth certificate is") : tr("Sign certificate is") ) << " ";
  276. if( c.isValid() )
  277. {
  278. s << "<font color=\"green\">" << tr("valid") << "</font>";
  279. if( willExpire )
  280. s << "<br /><font color=\"red\">" << tr("Your certificates will expire soon") << "</font>";
  281. }
  282. else
  283. s << "<font color=\"red\">" << tr("expired") << "</font>";
  284. s << "</td><td align=\"center\" width=\"75\">";
  285. if( !c.isValid() || willExpire )
  286. {
  287. s << "<a href=\"openUtility\"><img src=\":/images/warning.png\"><br />"
  288. "<font color=\"red\">" << tr("Open utility") << "</font></a>";
  289. }
  290. else if( c.isTempel() )
  291. s << "<img src=\":/images/ico_stamp_blue_75.png\">";
  292. else
  293. s << "<img src=\":/images/ico_person_blue_75.png\">";
  294. s << "</td></tr></table>";
  295. return content;
  296. }