Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 1 | /************************************************************************** |
Sébastien Blin | 68abac9 | 2019-01-02 17:41:31 -0500 | [diff] [blame] | 2 | | Copyright (C) 2015-2019 by Savoir-faire Linux | |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 3 | | Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> | |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 4 | | Author: Isa Nanic <isa.nanic@savoirfairelinux.com> | |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 5 | | | |
| 6 | | This program is free software; you can redistribute it and/or modify | |
| 7 | | it under the terms of the GNU General Public License as published by | |
| 8 | | the Free Software Foundation; either version 3 of the License, or | |
| 9 | | (at your option) any later version. | |
| 10 | | | |
| 11 | | This program is distributed in the hope that it will be useful, | |
| 12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
| 14 | | GNU General Public License for more details. | |
| 15 | | | |
| 16 | | You should have received a copy of the GNU General Public License | |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 17 | | along with this program. If not, see <https://www.gnu.org/licenses/>. | |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 18 | **************************************************************************/ |
| 19 | #pragma once |
| 20 | |
| 21 | #ifdef _MSC_VER |
| 22 | #undef ERROR |
| 23 | #endif |
| 24 | |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 25 | #include <QSettings> |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 26 | #include <QRegularExpression> |
| 27 | #include <QPixmap> |
| 28 | #include <QBuffer> |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 29 | |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 30 | #include "api/lrc.h" |
| 31 | #include "api/account.h" |
| 32 | #include "api/newaccountmodel.h" |
| 33 | #include "api/newcallmodel.h" |
| 34 | #include "api/behaviorcontroller.h" |
| 35 | #include "api/conversation.h" |
| 36 | #include "api/contactmodel.h" |
| 37 | #include "api/contact.h" |
| 38 | #include "api/datatransfermodel.h" |
Andreas Traczyk | db3894b | 2018-10-24 16:46:26 -0400 | [diff] [blame] | 39 | #include "api/conversationmodel.h" |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 40 | #include "accountlistmodel.h" |
| 41 | |
| 42 | #include "account.h" |
| 43 | //#include "instancemanager.cpp" |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 44 | |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 45 | #include <settingskey.h> |
| 46 | |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 47 | class LRCInstance { |
| 48 | public: |
| 49 | static void init() { |
| 50 | instance(); |
| 51 | }; |
| 52 | static lrc::api::Lrc& getAPI() { |
| 53 | return *(instance().lrc_); |
| 54 | }; |
| 55 | static void connectivityChanged() { |
| 56 | instance().lrc_->connectivityChanged(); |
| 57 | }; |
| 58 | static const lrc::api::NewAccountModel& accountModel() { |
| 59 | return instance().lrc_->getAccountModel(); |
| 60 | }; |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 61 | static lrc::api::NewAccountModel* editableAccountModel() { |
| 62 | return const_cast<lrc::api::NewAccountModel*>(&instance().lrc_->getAccountModel()); |
| 63 | }; |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 64 | static const lrc::api::BehaviorController& behaviorController() { |
| 65 | return instance().lrc_->getBehaviorController(); |
| 66 | }; |
| 67 | static const lrc::api::DataTransferModel& dataTransferModel() { |
| 68 | return instance().lrc_->getDataTransferModel(); |
| 69 | }; |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 70 | static lrc::api::DataTransferModel* editableDataTransferModel() { |
| 71 | return const_cast<lrc::api::DataTransferModel*>(&instance().lrc_->getDataTransferModel()); |
| 72 | }; |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 73 | static bool isConnected() { |
| 74 | return instance().lrc_->isConnected(); |
| 75 | }; |
| 76 | |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 77 | static const lrc::api::account::Info& |
| 78 | getCurrentAccountInfo() { |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 79 | return accountModel().getAccountInfo(getCurrAccId()); |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | static lrc::api::ConversationModel* |
| 83 | getCurrentConversationModel() { |
| 84 | return getCurrentAccountInfo().conversationModel.get(); |
| 85 | }; |
| 86 | |
| 87 | static lrc::api::NewCallModel* |
| 88 | getCurrentCallModel() { |
| 89 | return getCurrentAccountInfo().callModel.get(); |
| 90 | }; |
| 91 | |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 92 | static const int getAccountNumList() { |
| 93 | return accountModel().getAccountList().size(); |
| 94 | }; |
| 95 | |
| 96 | static const std::string& getCurrAccId() { |
Andreas Traczyk | 9b1b7d4 | 2018-12-13 14:44:24 -0500 | [diff] [blame] | 97 | if (instance().selectedAccountId.empty()) { |
| 98 | instance().selectedAccountId = accountModel().getAccountList().at(0); |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 99 | } |
Andreas Traczyk | 9b1b7d4 | 2018-12-13 14:44:24 -0500 | [diff] [blame] | 100 | return instance().selectedAccountId; |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | static void setSelectedAccountId(const std::string& accountId) { |
| 104 | instance().selectedAccountId = accountId; |
| 105 | QSettings settings; |
| 106 | settings.setValue(SettingsKey::selectedAccount, QString::fromStdString(accountId)); |
| 107 | }; |
| 108 | |
| 109 | static const std::string& getSelectedConvUid() { |
| 110 | return instance().selectedConvUid; |
| 111 | }; |
| 112 | |
| 113 | static void setSelectedConvId(const std::string& convUid) { |
| 114 | instance().selectedConvUid = convUid; |
| 115 | }; |
| 116 | |
Andreas Traczyk | 43c0823 | 2018-10-31 13:42:09 -0400 | [diff] [blame] | 117 | static void reset(bool newInstance = false) { |
| 118 | if (newInstance) { |
| 119 | instance().lrc_.reset(new lrc::api::Lrc()); |
| 120 | } else { |
| 121 | instance().lrc_.reset(); |
| 122 | } |
| 123 | }; |
| 124 | |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 125 | static const int getCurrentAccountIndex(){ |
| 126 | for (int i = 0; i < accountModel().getAccountList().size(); i++) { |
| 127 | if (accountModel().getAccountList()[i] == getCurrAccId()) { |
| 128 | return i; |
| 129 | } |
| 130 | } |
| 131 | return -1; |
| 132 | }; |
| 133 | |
| 134 | static const QPixmap getCurrAccPixmap() { |
| 135 | return instance().accountListModel_.data(instance().accountListModel_ |
| 136 | .index(getCurrentAccountIndex()), AccountListModel::Role::Picture).value<QPixmap>(); |
| 137 | }; |
| 138 | |
Andreas Traczyk | 6c323c6 | 2018-12-26 13:04:22 -0500 | [diff] [blame] | 139 | static void setCurrAccAvatar(const QPixmap& avatarPixmap) { |
| 140 | QByteArray ba; |
| 141 | QBuffer bu(&ba); |
| 142 | bu.open(QIODevice::WriteOnly); |
| 143 | avatarPixmap.save(&bu, "PNG"); |
| 144 | instance().editableAccountModel()->setAvatar(getCurrAccId(), ba.toBase64().toStdString()); |
| 145 | }; |
| 146 | |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 147 | static void setCurrAccAvatar(const std::string& avatar) { |
| 148 | instance().editableAccountModel()->setAvatar(getCurrAccId(), avatar); |
| 149 | }; |
| 150 | |
| 151 | static void setCurrAccDisplayName(const std::string& alias) { |
| 152 | instance().editableAccountModel()->setAlias(getCurrAccId(), alias); |
| 153 | }; |
| 154 | |
| 155 | static const lrc::api::account::ConfProperties_t& getCurrAccConfig() { |
| 156 | return instance().getCurrentAccountInfo().confProperties; |
| 157 | } |
| 158 | |
| 159 | |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 160 | private: |
| 161 | std::unique_ptr<lrc::api::Lrc> lrc_; |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 162 | AccountListModel accountListModel_; |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 163 | |
| 164 | static LRCInstance& instance() { |
| 165 | static LRCInstance instance_; |
| 166 | return instance_; |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 167 | }; |
Andreas Traczyk | fdc7f9b | 2018-08-14 18:42:03 -0400 | [diff] [blame] | 168 | |
| 169 | LRCInstance() { |
| 170 | lrc_ = std::make_unique<lrc::api::Lrc>(); |
| 171 | }; |
Andreas Traczyk | b8b13ba | 2018-08-21 16:30:16 -0400 | [diff] [blame] | 172 | |
| 173 | std::string selectedAccountId; |
| 174 | std::string selectedConvUid; |
Isa Nanic | 6e4a39a | 2018-12-04 14:26:02 -0500 | [diff] [blame] | 175 | }; |