PageRenderTime 104ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/kadu-core/misc/misc.cpp

https://github.com/ziemniak/kadu
C++ | 417 lines | 262 code | 28 blank | 127 comment | 38 complexity | 84804a2b1abc628e6c47f67e9a2c3f7b MD5 | raw file
Possible License(s): AGPL-1.0
  1. /***************************************************************************
  2. * *
  3. * This program is free software; you can redistribute it and/or modify *
  4. * it under the terms of the GNU General Public License as published by *
  5. * the Free Software Foundation; either version 2 of the License, or *
  6. * (at your option) any later version. *
  7. * *
  8. ***************************************************************************/
  9. #include <QtCore/QProcess>
  10. #include <QtCore/QUrl>
  11. #include <QtGui/QApplication>
  12. #include <QtGui/QDesktopServices>
  13. #include "accounts/account.h"
  14. #include "accounts/account-manager.h"
  15. #include "configuration/configuration-file.h"
  16. #include "contacts/contact-list.h"
  17. #include "contacts/contact-manager.h"
  18. #include "gui/widgets/chat-widget-manager.h"
  19. #include "gui/windows/message-box.h"
  20. #include "debug.h"
  21. #include "html_document.h"
  22. #include "misc.h"
  23. QFont *defaultFont;
  24. QFontInfo *defaultFontInfo;
  25. long int startTime, beforeExecTime, endingTime, exitingTime;
  26. bool measureTime = false;
  27. void saveWindowGeometry(const QWidget *w, const QString &section, const QString &name)
  28. {
  29. #ifdef Q_OS_MACX
  30. /* Dorr: on Mac make sure the window will not be greater than desktop */
  31. config_file.writeEntry(section, name,
  32. QApplication::desktop()->availableGeometry().intersected(w->geometry()));
  33. #else
  34. config_file.writeEntry(section, name,w->geometry());
  35. #endif
  36. }
  37. void loadWindowGeometry(QWidget *w, const QString &section, const QString &name, int defaultX, int defaultY, int defaultWidth, int defaultHeight)
  38. {
  39. QRect rect = config_file.readRectEntry(section, name);
  40. if ((rect.height() == 0) || (rect.width() == 0))
  41. {
  42. rect.setRect(defaultX, defaultY, defaultWidth, defaultHeight);
  43. }
  44. #ifdef Q_OS_MAC
  45. if (rect.y() < 20)
  46. rect.setY(20);
  47. #endif
  48. w->setGeometry(rect);
  49. }
  50. QStringList toStringList(const QString &e1, const QString &e2, const QString &e3, const QString &e4, const QString &e5)
  51. {
  52. QStringList list(e1);
  53. if (e2!=QString::null)
  54. list<<e2;
  55. if (e3!=QString::null)
  56. list<<e3;
  57. if (e4!=QString::null)
  58. list<<e4;
  59. if (e5!=QString::null)
  60. list<<e5;
  61. return list;
  62. }
  63. QString pwHash(const QString &text)
  64. {
  65. QString newText = text;
  66. for (unsigned int i = 0, textLength = text.length(); i < textLength; ++i)
  67. newText[i] = QChar(text[i].unicode() ^ i ^ 1);
  68. return newText;
  69. }
  70. QString translateLanguage(const QApplication *application, const QString &locale, const bool l2n)
  71. {
  72. static const int langSize = 5;
  73. static const char local[][3] = {"en",
  74. "de",
  75. "fr",
  76. "it",
  77. "pl"};
  78. static const char name[][sizeof("English") /*length of the longest*/] = {
  79. QT_TR_NOOP("English"),
  80. QT_TR_NOOP("German"),
  81. QT_TR_NOOP("French"),
  82. QT_TR_NOOP("Italian"),
  83. QT_TR_NOOP("Polish")};
  84. for (int i = 0; i < langSize; ++i)
  85. {
  86. if (l2n)
  87. {
  88. if (locale.mid(0, 2) == local[i])
  89. return application->translate("@default", name[i]);
  90. }
  91. else
  92. if (locale == application->translate("@default", name[i]))
  93. return local[i];
  94. }
  95. if (l2n)
  96. return application->translate("@default", QT_TR_NOOP("English"));
  97. else
  98. return "en";
  99. }
  100. void openWebBrowser(const QString &link)
  101. {
  102. kdebugf();
  103. QString webBrowser = config_file.readEntry("Chat", "WebBrowser", QString::null);
  104. if (webBrowser.isEmpty())
  105. {
  106. if(!QDesktopServices::openUrl(QUrl(link))){
  107. MessageBox::msg(qApp->translate("@default", QT_TR_NOOP("Web browser was not specified. Visit the configuration section")), false, "Warning");
  108. kdebugmf(KDEBUG_INFO, "Web browser NOT specified.\n");
  109. return;
  110. }
  111. else {
  112. kdebugf2();
  113. return;
  114. }
  115. }
  116. if (!webBrowser.contains("%1"))
  117. webBrowser.append(" \"" + link + '"');
  118. else
  119. webBrowser.replace("%1", link);
  120. QProcess *browser = new QProcess(qApp);
  121. browser->start(webBrowser);
  122. if (!browser->waitForStarted())
  123. MessageBox::msg(qApp->translate("@default", QT_TR_NOOP("Could not spawn Web browser process. Check if the Web browser is functional")), false, "Critical");
  124. kdebugf2();
  125. }
  126. void openMailClient(const QString &mail)
  127. {
  128. kdebugf();
  129. QString email = mail;
  130. QString mailClient = config_file.readEntry("Chat", "MailClient", QString::null);
  131. if (mailClient.isEmpty())
  132. {
  133. if(!mail.startsWith("mailto:"))
  134. email="mailto:"+mail;
  135. if(!QDesktopServices::openUrl(email)){
  136. MessageBox::msg(qApp->translate("@default", QT_TR_NOOP("Mail client was not specified. Visit the configuration section")), false, "Warning");
  137. kdebugmf(KDEBUG_INFO, "Mail client NOT specified.\n");
  138. return;
  139. }
  140. else
  141. {
  142. kdebugf2();
  143. return;
  144. }
  145. }
  146. if (email.startsWith("mailto:"))
  147. email.remove(0, 7); // usuwamy "mailto:", je�li zosta�o dodane jako fragment adresu
  148. if (mailClient.contains("%1"))
  149. mailClient.replace("%1", email);
  150. else
  151. mailClient.append(email);
  152. QProcess *mailer = new QProcess(qApp);
  153. mailer->start(mailClient);
  154. if (!mailer->waitForStarted())
  155. MessageBox::msg(qApp->translate("@default", QT_TR_NOOP("Could not spawn Mail client process. Check if the Mail client is functional")), false, "Critical");
  156. kdebugf2();
  157. }
  158. void openGGChat(const QString &gg)
  159. {
  160. kdebugf();
  161. QString gadu = gg;
  162. if (gadu.startsWith("gg:"))
  163. {
  164. gadu.remove(0, 3);
  165. gadu.remove(QRegExp("/*"));
  166. }
  167. Account *account = AccountManager::instance()->defaultAccount();
  168. ContactSet contacts(ContactManager::instance()->byId(account, gadu));
  169. // TODO: 0.6.6
  170. // chat_manager->openPendingMsgs(contacts);
  171. kdebugf2();
  172. }
  173. QString versionToName(const unsigned int version)
  174. {
  175. kdebugf();
  176. QString name;
  177. switch (version)
  178. {
  179. case 0x20: name = "GG 6.0b129"; break;
  180. case 0x21: name = "GG 6.0b133"; break;
  181. case 0x22: name = "GG 6.0b140"; break;
  182. case 0x24: name = "GG 6.1b155/7.6b1359"; break;
  183. case 0x25: name = "GG 7.0b1"; break;
  184. case 0x26: name = "GG 7.0b20"; break;
  185. case 0x27: name = "GG 7.0b22"; break;
  186. case 0x28: name = "GG 7.5b2201"; break;
  187. case 0x29: name = "GG 7.6b1688"; break;
  188. case 0x2a: name = "GG 7.7b3315"; break;
  189. case 0x2d: name = "GG 8.0b4881"; break;
  190. case 0x2e: name = "GG 8.0b8283"; break;
  191. default: name = "Unknown"; break;
  192. }
  193. return name;
  194. kdebugf2();
  195. }
  196. //internal usage
  197. static void stringHeapSortPushDown(QString *heap, int first, int last)
  198. {
  199. int r = first;
  200. while ( r <= last / 2 ) {
  201. if ( last == 2 * r ) {
  202. if ( heap[2 * r].localeAwareCompare(heap[r])<0 )
  203. qSwap( heap[r], heap[2 * r] );
  204. r = last;
  205. } else {
  206. if ( heap[2 * r].localeAwareCompare( heap[r] )<0 && !(heap[2 * r + 1].localeAwareCompare(heap[2 * r])<0) ) {
  207. qSwap( heap[r], heap[2 * r] );
  208. r *= 2;
  209. } else if ( heap[2 * r + 1].localeAwareCompare( heap[r] )<0 && heap[2 * r + 1].localeAwareCompare( heap[2 * r] )<0 ) {
  210. qSwap( heap[r], heap[2 * r + 1] );
  211. r = 2 * r + 1;
  212. } else {
  213. r = last;
  214. }
  215. }
  216. }
  217. }
  218. //internal usage
  219. static void stringHeapSortHelper( QStringList::iterator b, QStringList::iterator e, QString, uint n )
  220. {
  221. QStringList::iterator insert = b;
  222. QString *realheap = new QString[n];
  223. QString *heap = realheap - 1;
  224. int size = 0;
  225. for( ; insert != e; ++insert ) {
  226. heap[++size] = *insert;
  227. int i = size;
  228. while( i > 1 && heap[i].localeAwareCompare(heap[i / 2])<0 ) {
  229. qSwap( heap[i], heap[i / 2] );
  230. i /= 2;
  231. }
  232. }
  233. for( uint i = n; i > 0; --i ) {
  234. *b++ = heap[1];
  235. if ( i > 1 ) {
  236. heap[1] = heap[i];
  237. stringHeapSortPushDown( heap, 1, (int)i - 1 );
  238. }
  239. }
  240. delete[] realheap;
  241. }
  242. void stringHeapSort(QStringList &c)
  243. {
  244. if (c.begin() == c.end())
  245. return;
  246. stringHeapSortHelper(c.begin(), c.end(), *(c.begin()), (uint)c.count());
  247. }
  248. // -----------------------
  249. // TokenDialog
  250. // -----------------------
  251. // PixmapPreview::PixmapPreview() : QLabel(NULL)
  252. // {
  253. // }
  254. /*
  255. void PixmapPreview::previewUrl(const Q3Url& url)
  256. {
  257. QString path = url.path();
  258. QPixmap pix( path );
  259. if (pix.isNull())
  260. setText(qApp->translate("PixmapPreview", "This is not an image"));
  261. else
  262. {
  263. QMatrix mx;
  264. mx.scale(
  265. double(width())/double(pix.width()),
  266. double(height())/double(pix.height()));
  267. pix = pix.xForm(mx);
  268. setPixmap(pix);
  269. }
  270. }*/
  271. QList<int> toIntList(const QList<QVariant> &in)
  272. {
  273. QList<int> out;
  274. foreach(const QVariant &it, in)
  275. out.append(it.toInt());
  276. return out;
  277. }
  278. QList<QVariant> toVariantList(const QList<int> &in)
  279. {
  280. QList<QVariant> out;
  281. foreach(const int &it, in)
  282. out.append(QVariant(it));
  283. return out;
  284. }
  285. QRegExp clean_regexp;
  286. QString toPlainText(const QString &text)
  287. {
  288. kdebugm(KDEBUG_INFO, "rich: %s\n", qPrintable(text));
  289. if (clean_regexp.isEmpty())
  290. {
  291. clean_regexp = QRegExp("<.*>");
  292. clean_regexp.setMinimal(true);
  293. }
  294. QString copy=text;
  295. copy.replace("\r\n", " ");
  296. copy.replace("\n", " ");
  297. copy.replace("\r", " ");
  298. copy.remove(clean_regexp);
  299. HtmlDocument::unescapeText(copy);
  300. kdebugm(KDEBUG_INFO, "plain: %s\n", qPrintable(copy));
  301. return copy;
  302. }
  303. QRect stringToRect(const QString &value, const QRect *def)
  304. {
  305. QStringList stringlist;
  306. QRect rect(0,0,0,0);
  307. int l, t, w, h;
  308. bool ok;
  309. stringlist = value.split(',', QString::SkipEmptyParts);
  310. if (stringlist.count() != 4)
  311. return def ? *def : rect;
  312. l = stringlist[0].toInt(&ok); if (!ok) return def ? *def : rect;
  313. t = stringlist[1].toInt(&ok); if (!ok) return def ? *def : rect;
  314. w = stringlist[2].toInt(&ok); if (!ok) return def ? *def : rect;
  315. h = stringlist[3].toInt(&ok); if (!ok) return def ? *def : rect;
  316. rect.setRect(l, t, w, h);
  317. return rect;
  318. }
  319. QString rectToString(const QRect& rect)
  320. {
  321. return QString("%1,%2,%3,%4").arg(rect.left()).arg(rect.top()).arg(rect.width()).arg(rect.height());
  322. }
  323. QString narg(const QString &s, const QString **tab, int count)
  324. {
  325. kdebugf();
  326. QString out;
  327. const QChar *d = s.unicode();
  328. const QChar *dend = d + s.length();
  329. int j = 0;
  330. char maxc = '0' + count;
  331. if (count > 9)
  332. return QString::null;
  333. while (d != dend)
  334. {
  335. if (*d == '%' && d + 1 < dend && *(d + 1) >= '1' && *(d + 1) <= maxc)
  336. {
  337. out.append(QString(d - j, j));
  338. ++d;
  339. out.append(*(tab[d->digitValue() - 1]));
  340. j = 0;
  341. }
  342. else
  343. ++j;
  344. ++d;
  345. }
  346. out.append(QString(d - j, j));
  347. // kdebugm(KDEBUG_DUMP, "out: '%s'\n", qPrintable(out));
  348. kdebugf2();
  349. return out;
  350. }
  351. QString narg(const QString &s, const QString &arg1, const QString &arg2,
  352. const QString &arg3, const QString &arg4,
  353. const QString &arg5, const QString &arg6,
  354. const QString &arg7, const QString &arg8,
  355. const QString &arg9)
  356. {
  357. const QString *tab[9]={&arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8, &arg9};
  358. return narg(s, tab, 9);
  359. }
  360. QString narg(const QString &s, const QString &arg1, const QString &arg2, const QString &arg3, const QString &arg4)
  361. {
  362. const QString *tab[4]={&arg1, &arg2, &arg3, &arg4};
  363. return narg(s, tab, 4);
  364. }