PageRenderTime 862ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/qutim-0.3.1/protocols/vkontakte/src/vkontakteprotocol.cpp

#
C++ | 149 lines | 103 code | 16 blank | 30 comment | 3 complexity | ed0e1f2900b13aa8ee729757c7bd4790 MD5 | raw file
Possible License(s): CC-BY-SA-4.0, GPL-3.0, LGPL-2.1, GPL-2.0
  1. /****************************************************************************
  2. **
  3. ** qutIM - instant messenger
  4. **
  5. ** Copyright Š 2011 Aleksey Sidorov <gorthauer87@yandex.ru>
  6. **
  7. *****************************************************************************
  8. **
  9. ** $QUTIM_BEGIN_LICENSE$
  10. ** This program is free software: you can redistribute it and/or modify
  11. ** it under the terms of the GNU General Public License as published by
  12. ** the Free Software Foundation, either version 3 of the License, or
  13. ** (at your option) any later version.
  14. **
  15. ** This program is distributed in the hope that it will be useful,
  16. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. ** See the GNU General Public License for more details.
  19. **
  20. ** You should have received a copy of the GNU General Public License
  21. ** along with this program. If not, see http://www.gnu.org/licenses/.
  22. ** $QUTIM_END_LICENSE$
  23. **
  24. ****************************************************************************/
  25. #include "vkontakteprotocol.h"
  26. #include "vkontakteprotocol_p.h"
  27. #include <qutim/account.h>
  28. #include "vaccount.h"
  29. #include <qutim/statusactiongenerator.h>
  30. #include "vcontact.h"
  31. #include <QDesktopServices>
  32. #include <QUrl>
  33. #include <qutim/icon.h>
  34. #include <QInputDialog>
  35. #include <qutim/message.h>
  36. #include <QDateTime>
  37. #include <qutim/chatsession.h>
  38. #include "vmessages.h"
  39. #include "vconnection.h"
  40. #include <qutim/settingslayer.h>
  41. #include "ui/vaccountsettings.h"
  42. VkontakteProtocol *VkontakteProtocol::self = 0;
  43. VkontakteProtocol::VkontakteProtocol() :
  44. d_ptr(new VkontakteProtocolPrivate)
  45. {
  46. Q_ASSERT(!self);
  47. self = this;
  48. d_func()->q_ptr = this;
  49. }
  50. VkontakteProtocol::~VkontakteProtocol()
  51. {
  52. Settings::removeItem(m_mainSettings);
  53. delete m_mainSettings;
  54. m_mainSettings = 0;
  55. foreach (VAccount *account, d_func()->accounts)
  56. account->saveSettings();
  57. self = 0;
  58. }
  59. Account* VkontakteProtocol::account(const QString& id) const
  60. {
  61. Q_D(const VkontakteProtocol);
  62. return d->accounts.value(id);
  63. }
  64. QList< Account* > VkontakteProtocol::accounts() const
  65. {
  66. Q_D(const VkontakteProtocol);
  67. AccountList accounts;
  68. VAccountHash::const_iterator it;
  69. for (it = d->accounts.begin(); it != d->accounts.end(); it++)
  70. accounts.append(it.value());
  71. return accounts;
  72. }
  73. void VkontakteProtocol::loadAccounts()
  74. {
  75. Q_D(VkontakteProtocol);
  76. QList<Status> statuses;
  77. statuses << Status(Status::Online)
  78. << Status(Status::Offline);
  79. foreach (Status status, statuses) {
  80. status.initIcon("vkontakte");
  81. Status::remember(status, "vkontakte");
  82. MenuController::addAction(new StatusActionGenerator(status), &VAccount::staticMetaObject);
  83. }
  84. ActionGenerator *gen = new ActionGenerator(Icon("applications-internet"),
  85. QT_TRANSLATE_NOOP("Vkontakte","Open homepage"),
  86. d,
  87. SLOT(onOpenWebPageTriggered(QObject*)));
  88. gen->setType(ActionTypeContactList);
  89. MenuController::addAction<VContact>(gen);
  90. // static ActionGenerator sms_gen(Icon("phone"),
  91. // QT_TRANSLATE_NOOP("Vkontakte","Send sms"),
  92. // d,
  93. // SLOT(onSendSmsTriggered(QObject*)));
  94. // sms_gen.setType(ActionTypeContactList);
  95. // MenuController::addAction<VContact>(&sms_gen);
  96. QStringList accounts = config("general").value("accounts", QStringList());
  97. foreach (const QString &uid, accounts) {
  98. VAccount *acc = new VAccount(uid, this);
  99. d->accounts.insert(uid, acc);
  100. acc->loadSettings();
  101. connect(acc, SIGNAL(destroyed(QObject*)), d, SLOT(onAccountDestroyed(QObject*)));
  102. emit accountCreated(acc);
  103. }
  104. m_mainSettings = new GeneralSettingsItem<VAccountSettings>(Settings::Protocol,Icon("im-jabber"),QT_TRANSLATE_NOOP("Vkontakte","Account settings"));
  105. Settings::registerItem<VAccount>(m_mainSettings);
  106. }
  107. QVariant VkontakteProtocol::data(DataType type)
  108. {
  109. switch (type) {
  110. case ProtocolIdName:
  111. return tr("id");
  112. case ProtocolContainsContacts:
  113. return true;
  114. default:
  115. return QVariant();
  116. }
  117. }
  118. void VkontakteProtocolPrivate::onAccountDestroyed(QObject *obj)
  119. {
  120. VAccount *account = static_cast<VAccount*>(obj);
  121. accounts.remove(accounts.key(account));
  122. }
  123. void VkontakteProtocolPrivate::onOpenWebPageTriggered(QObject *obj)
  124. {
  125. VContact *con = qobject_cast<VContact*>(obj);
  126. Q_ASSERT(obj);
  127. QUrl url ("http://vkontakte.ru/id" + con->id());
  128. QDesktopServices::openUrl(url);
  129. }
  130. bool VkontakteProtocol::event(QEvent *ev)
  131. {
  132. return Protocol::event(ev);
  133. }