blob: 57fe69b5bc49e3cc11835a78e2a1a5cb38b750ee [file] [log] [blame]
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -04001/**************************************************************************
2| Copyright (C) 2015-2018 by Savoir-faire Linux |
3| Author: Andreas Traczyk <andreas.traczyk@savoirfairelinux.com> |
Isa Nanic6e4a39a2018-12-04 14:26:02 -05004| Author: Isa Nanic <isa.nanic@savoirfairelinux.com> |
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -04005| |
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 Nanic6e4a39a2018-12-04 14:26:02 -050017| along with this program. If not, see <https://www.gnu.org/licenses/>. |
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -040018**************************************************************************/
19#pragma once
20
21#ifdef _MSC_VER
22#undef ERROR
23#endif
24
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040025#include <QSettings>
Isa Nanic6e4a39a2018-12-04 14:26:02 -050026#include <QRegularExpression>
27#include <QPixmap>
28#include <QBuffer>
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040029
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -040030#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 Traczykdb3894b2018-10-24 16:46:26 -040039#include "api/conversationmodel.h"
Isa Nanic6e4a39a2018-12-04 14:26:02 -050040#include "accountlistmodel.h"
41
42#include "account.h"
43//#include "instancemanager.cpp"
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -040044
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040045#include <settingskey.h>
46
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -040047class LRCInstance {
48public:
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 Nanic6e4a39a2018-12-04 14:26:02 -050061 static lrc::api::NewAccountModel* editableAccountModel() {
62 return const_cast<lrc::api::NewAccountModel*>(&instance().lrc_->getAccountModel());
63 };
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -040064 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 Nanic6e4a39a2018-12-04 14:26:02 -050070 static lrc::api::DataTransferModel* editableDataTransferModel() {
71 return const_cast<lrc::api::DataTransferModel*>(&instance().lrc_->getDataTransferModel());
72 };
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -040073 static bool isConnected() {
74 return instance().lrc_->isConnected();
75 };
76
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040077 static const lrc::api::account::Info&
78 getCurrentAccountInfo() {
Isa Nanic6e4a39a2018-12-04 14:26:02 -050079 return accountModel().getAccountInfo(getCurrAccId());
Andreas Traczykb8b13ba2018-08-21 16:30:16 -040080 };
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 Nanic6e4a39a2018-12-04 14:26:02 -050092 static const int getAccountNumList() {
93 return accountModel().getAccountList().size();
94 };
95
96 static const std::string& getCurrAccId() {
97 if (!instance().selectedAccountId.empty()) {
98 return instance().selectedAccountId;
99 }
100 return accountModel().getAccountList()[0];
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400101 };
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 Traczyk43c08232018-10-31 13:42:09 -0400117 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 Nanic6e4a39a2018-12-04 14:26:02 -0500125 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
139 static void setCurrAccAvatar(const std::string& avatar) {
140 instance().editableAccountModel()->setAvatar(getCurrAccId(), avatar);
141 };
142
143 static void setCurrAccDisplayName(const std::string& alias) {
144 instance().editableAccountModel()->setAlias(getCurrAccId(), alias);
145 };
146
147 static const lrc::api::account::ConfProperties_t& getCurrAccConfig() {
148 return instance().getCurrentAccountInfo().confProperties;
149 }
150
151
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -0400152private:
153 std::unique_ptr<lrc::api::Lrc> lrc_;
Isa Nanic6e4a39a2018-12-04 14:26:02 -0500154 AccountListModel accountListModel_;
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -0400155
156 static LRCInstance& instance() {
157 static LRCInstance instance_;
158 return instance_;
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400159 };
Andreas Traczykfdc7f9b2018-08-14 18:42:03 -0400160
161 LRCInstance() {
162 lrc_ = std::make_unique<lrc::api::Lrc>();
163 };
Andreas Traczykb8b13ba2018-08-21 16:30:16 -0400164
165 std::string selectedAccountId;
166 std::string selectedConvUid;
Isa Nanic6e4a39a2018-12-04 14:26:02 -0500167};