1 /***************************************************************************
2  *   Copyright (C) 2011 by Lasath Fernando <kde@lasath.org>                *
3  *   Copyright (C) 2011 by David Edmundson <kde@davidedmundson.co.uk>      *
4  *   Copyright (C) 2013 by Stefan Eggers <coloncolonone@gmail.com>         *
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 2 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     *
17  *   along with this program; if not, write to the                         *
18  *   Free Software Foundation, Inc.,                                       *
19  *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
20  ***************************************************************************/
21 
22 
23 #include <KConfigGroup>
24 #include <KSharedConfig>
25 
26 #include "text-chat-config.h"
27 
28 #include "shareprovider.h"
29 
30 QMutex TextChatConfig::mutex(QMutex::Recursive);
31 
32 
33 class TextChatConfigPrivate
34 {
35 public:
36     TextChatConfig::TabOpenMode m_openMode;
37     int m_scrollbackLength;
38     bool m_showMeTyping;
39     bool m_showOthersTyping;
40     bool m_dontLeaveGroupChats;
41     bool m_rememberTabKeyboardLayout;
42     QString m_nicknameCompletionSuffix;
43     ShareProvider::ShareService m_imageShareServiceType;
44 };
45 
46 
instance()47 TextChatConfig *TextChatConfig::instance()
48 {
49     static TextChatConfig *instance = 0;
50 
51     mutex.lock();
52 
53     if (!instance) {
54         instance = new TextChatConfig();
55     }
56 
57     mutex.unlock();
58 
59     // if instance was already setup the mutex protected block won't have
60     // changed it; if we were the first and instance was still 0 we set it up
61     // inside the mutex protected block and instance won't ever change again;
62     // thus this is allowed to be outside the mutex protected block
63     return instance;
64 }
65 
66 
sync()67 void TextChatConfig::sync()
68 {
69     mutex.lock();
70 
71     KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("ktelepathyrc"));
72     KConfigGroup behaviorConfig = config->group("Behavior");
73 
74     QString mode;
75     if (d->m_openMode == TextChatConfig::NewWindow) {
76         mode = QLatin1String("NewWindow");
77     } else {
78         mode = QLatin1String("FirstWindow");
79     }
80     behaviorConfig.writeEntry("tabOpenMode", mode);
81 
82     behaviorConfig.writeEntry("scrollbackLength", d->m_scrollbackLength);
83 
84     behaviorConfig.writeEntry("showMeTyping", d->m_showMeTyping);
85 
86     behaviorConfig.writeEntry("showOthersTyping", d->m_showOthersTyping);
87 
88     behaviorConfig.writeEntry("nicknameCompletionSuffix", d->m_nicknameCompletionSuffix);
89 
90     behaviorConfig.writeEntry("imageShareServiceType", static_cast<int>(d->m_imageShareServiceType));
91 
92     behaviorConfig.writeEntry("dontLeaveGroupChats", d->m_dontLeaveGroupChats);
93 
94     behaviorConfig.writeEntry("rememberTabKeyboardLayout", d->m_rememberTabKeyboardLayout);
95 
96     behaviorConfig.sync();
97 
98     mutex.unlock();
99 }
100 
101 
openMode()102 TextChatConfig::TabOpenMode TextChatConfig::openMode()
103 {
104     TextChatConfig::TabOpenMode result;
105 
106     mutex.lock();
107     result = d->m_openMode;
108     mutex.unlock();
109 
110     return result;
111 }
112 
113 
setOpenMode(TextChatConfig::TabOpenMode mode)114 void TextChatConfig::setOpenMode(TextChatConfig::TabOpenMode mode)
115 {
116     mutex.lock();
117     d->m_openMode = mode;
118     mutex.unlock();
119 }
120 
121 
scrollbackLength()122 int TextChatConfig::scrollbackLength()
123 {
124     int result;
125 
126     mutex.lock();
127     result = d->m_scrollbackLength;
128     mutex.unlock();
129 
130     return result;
131 }
132 
133 
setScrollbackLength(int length)134 void TextChatConfig::setScrollbackLength(int length)
135 {
136     mutex.lock();
137     d->m_scrollbackLength = length;
138     mutex.unlock();
139 }
140 
141 
showMeTyping()142 bool TextChatConfig::showMeTyping()
143 {
144     bool result;
145 
146     mutex.lock();
147     result = d->m_showMeTyping;
148     mutex.unlock();
149 
150     return result;
151 }
152 
153 
setShowMeTyping(bool showTyping)154 void TextChatConfig::setShowMeTyping(bool showTyping)
155 {
156     mutex.lock();
157     d->m_showMeTyping = showTyping;
158     mutex.unlock();
159 }
160 
161 
showOthersTyping()162 bool TextChatConfig::showOthersTyping()
163 {
164     bool result;
165 
166     mutex.lock();
167     result = d->m_showOthersTyping;
168     mutex.unlock();
169 
170     return result;
171 }
172 
173 
setShowOthersTyping(bool showTyping)174 void TextChatConfig::setShowOthersTyping(bool showTyping)
175 {
176     mutex.lock();
177     d->m_showOthersTyping = showTyping;
178     mutex.unlock();
179 }
180 
181 
nicknameCompletionSuffix() const182 QString TextChatConfig::nicknameCompletionSuffix() const {
183     return d->m_nicknameCompletionSuffix;
184 }
185 
setNicknameCompletionSuffix(const QString & suffix)186 void TextChatConfig::setNicknameCompletionSuffix(const QString &suffix) {
187     mutex.lock();
188     d->m_nicknameCompletionSuffix = suffix;
189     mutex.unlock();
190 }
191 
imageShareServiceType() const192 ShareProvider::ShareService TextChatConfig::imageShareServiceType() const
193 {
194     return d->m_imageShareServiceType;
195 }
196 
setImageShareServiceName(ShareProvider::ShareService serviceType)197 void TextChatConfig::setImageShareServiceName(ShareProvider::ShareService serviceType)
198 {
199     mutex.lock();
200     d->m_imageShareServiceType = serviceType;
201     mutex.unlock();
202 }
203 
204 
dontLeaveGroupChats() const205 bool TextChatConfig::dontLeaveGroupChats() const
206 {
207     return d->m_dontLeaveGroupChats;
208 }
209 
setDontLeaveGroupChats(bool dontLeaveGroupChats)210 void TextChatConfig::setDontLeaveGroupChats(bool dontLeaveGroupChats)
211 {
212     mutex.lock();
213     d->m_dontLeaveGroupChats = dontLeaveGroupChats;
214     mutex.unlock();
215 }
216 
rememberTabKeyboardLayout() const217 bool TextChatConfig::rememberTabKeyboardLayout() const
218 {
219     return d->m_rememberTabKeyboardLayout;
220 }
221 
setRememberTabKeyboardLayout(bool change)222 void TextChatConfig::setRememberTabKeyboardLayout(bool change)
223 {
224     mutex.lock();
225     d->m_rememberTabKeyboardLayout = change;
226     mutex.unlock();
227 }
228 
TextChatConfig()229 TextChatConfig::TextChatConfig() :
230     d(new TextChatConfigPrivate())
231 {
232     // only ever called from TextChatConfig::instance() while mutex is locked
233     // thus no own mutex use inside the constructor
234 
235     KSharedConfigPtr config = KSharedConfig::openConfig(QLatin1String("ktelepathyrc"));
236     KConfigGroup behaviorConfig = config->group("Behavior");
237 
238     QString mode = behaviorConfig.readEntry("tabOpenMode", "FirstWindow");
239     if(mode == QLatin1String("NewWindow")) {
240         d->m_openMode = TextChatConfig::NewWindow;
241     } else {
242         d->m_openMode = TextChatConfig::FirstWindow;
243     }
244 
245     d->m_scrollbackLength = behaviorConfig.readEntry("scrollbackLength", 4);
246 
247     d->m_showMeTyping = behaviorConfig.readEntry("showMeTyping", true);
248 
249     d->m_showOthersTyping = behaviorConfig.readEntry("showOthersTyping", true);
250 
251     d->m_nicknameCompletionSuffix = behaviorConfig.readEntry("nicknameCompletionSuffix", ", ");
252 
253     d->m_dontLeaveGroupChats = behaviorConfig.readEntry("dontLeaveGroupChats", false);
254 
255     d->m_rememberTabKeyboardLayout = behaviorConfig.readEntry("rememberTabKeyboardLayout", false);
256 
257     // Imgur is the default image sharing service
258     int shareServiceType = behaviorConfig.readEntry("imageShareServiceType", static_cast<int>(ShareProvider::Imgur));
259     d->m_imageShareServiceType = static_cast<ShareProvider::ShareService>(shareServiceType);
260 }
261 
~TextChatConfig()262 TextChatConfig::~TextChatConfig()
263 {}
264