core.cpp 48.2 KB
Newer Older
1 2
/*
 * %kadu copyright begin%
Rafał Malinowski's avatar
Rafał Malinowski committed
3 4
 * Copyright 2011 Tomasz Rostanski (rozteck@interia.pl)
 * Copyright 2009, 2010, 2010, 2011 Piotr Galiszewski (piotr.galiszewski@kadu.im)
5 6
 * Copyright 2010, 2010, 2011 Przemysław Rudy (prudy1@o2.pl)
 * Copyright 2009, 2010, 2010, 2011, 2012 Wojciech Treter (juzefwt@gmail.com)
Rafał Malinowski's avatar
Rafał Malinowski committed
7 8
 * Copyright 2010, 2010, 2010 Tomasz Rostański (rozteck@interia.pl)
 * Copyright 2010, 2011 Piotr Dąbrowski (ultr@ultr.pl)
9
 * Copyright 2009 Michał Podsiadlik (michal@kadu.net)
Rafał Malinowski's avatar
Rafał Malinowski committed
10
 * Copyright 2009, 2010 Maciej Płaza (plaza.maciej@gmail.com)
11
 * Copyright 2009 Bartłomiej Zimoń (uzi18@o2.pl)
Rafał Malinowski's avatar
Rafał Malinowski committed
12
 * Copyright 2010 badboy (badboy@gen2.org)
13 14
 * Copyright 2009, 2009, 2010, 2011, 2012, 2013 Rafał Malinowski (rafal.przemyslaw.malinowski@gmail.com)
 * Copyright 2010, 2011, 2012, 2013 Bartosz Brachaczek (b.brachaczek@gmail.com)
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 * %kadu copyright end%
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
Rafał Malinowski's avatar
Rafał Malinowski committed
30

Rafał Malinowski's avatar
Rafał Malinowski committed
31
#include <QtCore/QDir>
Piotr Galiszewski's avatar
Piotr Galiszewski committed
32
#include <QtCore/QLocale>
33
#include <QtCore/QSettings>
34
#include <QtCore/QTimer>
35
#include <QtWidgets/QApplication>
36

Rafał Malinowski's avatar
Rafał Malinowski committed
37
#include "accounts/account-manager.h"
38
#include "avatars/avatar-manager.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
39 40
#include "buddies/buddy-manager.h"
#include "buddies/group-manager.h"
41
#include "chat/buddy-chat-manager.h"
42
#include "chat/chat-manager.h"
43
#include "chat-style/chat-style-manager.h"
44 45
#include "chat-style/engine/chat-style-renderer-factory-provider.h"
#include "chat-style/engine/configured-chat-style-renderer-factory-provider.h"
46
#include "configuration/deprecated-configuration-api.h"
47
#include "configuration/configuration-manager.h"
48
#include "configuration/main-configuration-holder.h"
49
#include "contacts/contact-manager.h"
50
#include "dom/dom-processor-service.h"
51
#include "file-transfer/file-transfer-manager.h"
52
#include "formatted-string/formatted-string-factory.h"
53
#include "gui/services/clipboard-html-transformer-service.h"
54
#include "gui/widgets/account-configuration-widget-factory-repository.h"
55
#include "gui/widgets/buddy-configuration-widget-factory-repository.h"
56
#include "gui/widgets/chat-configuration-widget-factory-repository.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
57
#include "gui/widgets/chat-edit-box.h"
58
#include "gui/widgets/chat-top-bar-widget-factory-repository.h"
59
#include "gui/widgets/chat-widget/chat-widget-actions.h"
60
#include "gui/widgets/chat-widget/chat-widget-activation-service.h"
61
#include "gui/widgets/chat-widget/chat-widget-container-handler-mapper.h"
62
#include "gui/widgets/chat-widget/chat-widget-container-handler-repository.h"
63
#include "gui/widgets/chat-widget/chat-widget-factory.h"
64
#include "gui/widgets/chat-widget/chat-widget-manager.h"
65
#include "gui/widgets/chat-widget/chat-widget-message-handler.h"
66
#include "gui/widgets/chat-widget/chat-widget-message-handler-configurator.h"
67
#include "gui/widgets/chat-widget/chat-widget-repository.h"
68
#include "gui/widgets/webkit-messages-view/webkit-messages-view-display-factory.h"
69 70
#include "gui/widgets/webkit-messages-view/webkit-messages-view-factory.h"
#include "gui/widgets/webkit-messages-view/webkit-messages-view-handler-factory.h"
71
#include "gui/windows/buddy-data-window-repository.h"
72
#include "gui/windows/chat-data-window-repository.h"
73
#include "gui/windows/chat-window/chat-window-factory.h"
74
#include "gui/windows/chat-window/chat-window-manager.h"
75 76 77
#include "gui/windows/chat-window/chat-window-storage.h"
#include "gui/windows/chat-window/chat-window-storage-configurator.h"
#include "gui/windows/chat-window/chat-window-repository.h"
78
#include "gui/windows/chat-window/window-chat-widget-container-handler.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
79
#include "gui/windows/kadu-window.h"
80
#include "gui/windows/search-window.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
81
#include "icons/icons-manager.h"
82
#include "icons/kadu-icon.h"
83
#include "message/message-html-renderer-service.h"
84
#include "message/message-manager.h"
85
#include "message/message-render-info.h"
86
#include "message/message-render-info-factory.h"
87
#include "message/unread-message-repository.h"
88
#include "misc/change-notifier-lock.h"
89
#include "misc/date-time-parser-tags.h"
90
#include "misc/kadu-paths.h"
91
#include "notify/notification-manager.h"
92
#include "parser/parser.h"
93
#include "plugin/dependency-graph/plugin-dependency-graph-builder.h"
94 95
#include "plugin/activation/plugin-activation-error-handler.h"
#include "plugin/activation/plugin-activation-service.h"
96 97
#include "plugin/metadata/plugin-metadata-finder.h"
#include "plugin/metadata/plugin-metadata-reader.h"
98
#include "plugin/plugin-conflict-resolver.h"
99
#include "plugin/plugin-dependency-handler.h"
100
#include "plugin/plugin-manager.h"
101
#include "plugin/state/plugin-state-manager.h"
102 103
#include "plugin/state/plugin-state-service.h"
#include "plugin/state/plugin-state-storage.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
104
#include "protocols/protocol-factory.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
105
#include "protocols/protocol.h"
106
#include "protocols/services/roster/roster-notifier.h"
107 108
#include "provider/default-provider.h"
#include "provider/simple-provider.h"
109
#include "services/chat-image-request-service-configurator.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
110
#include "services/chat-image-request-service.h"
111
#include "services/image-storage-service.h"
112
#include "services/message-filter-service.h"
113
#include "services/message-transformer-service.h"
Juzef's avatar
Juzef committed
114
#include "services/notification-service.h"
115
#include "services/raw-message-transformer-service.h"
116
#include "status/status-container-manager.h"
117
#include "status/status-setter.h"
118
#include "status/status-type-manager.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
119
#include "status/status-type.h"
120
#include "storage/storage-point-factory.h"
121
#include "themes/icon-theme-manager.h"
122
#include "url-handlers/url-handler-manager.h"
123
#include "activate.h"
124
#include "debug.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
125
#include "kadu-application.h"
126
#include "kadu-config.h"
127
#include "updates.h"
Rafał Malinowski's avatar
Rafał Malinowski committed
128

129
#if WITH_LIBINDICATE_QT
130 131 132
#include <libindicate-qt/qindicateserver.h>
#endif

Rafał Malinowski's avatar
Rafał Malinowski committed
133 134 135 136 137 138 139
#include "core.h"

Core * Core::Instance = 0;

Core * Core::instance()
{
	if (!Instance)
140
	{
Rafał Malinowski's avatar
Rafał Malinowski committed
141
		Instance = new Core();
142 143
		Instance->init();
	}
Rafał Malinowski's avatar
Rafał Malinowski committed
144 145 146 147

	return Instance;
}

148 149 150 151 152
QString Core::name()
{
	return QLatin1String("Kadu");
}

153 154
QString Core::version()
{
155
	return QLatin1String(KADU_VERSION);
156 157 158 159 160
}

QString Core::nameWithVersion()
{
	return name() + QLatin1String(" ")  + version();
161 162
}

163 164 165 166 167
KaduApplication * Core::application()
{
	return static_cast<KaduApplication *>(qApp);
}

168
Core::Core() :
169 170 171 172 173 174 175 176 177
		KaduWindowProvider{new SimpleProvider<QWidget *>(0)},
		MainWindowProvider{new DefaultProvider<QWidget *>(KaduWindowProvider)},
		CurrentBuddyDataWindowRepository{nullptr},
		CurrentChatDataWindowRepository{nullptr},
		CurrentChatImageRequestService{nullptr},
		CurrentDomProcessorService{nullptr},
		CurrentImageStorageService{nullptr},
		CurrentMessageFilterService{nullptr},
		CurrentMessageHtmlRendererService{nullptr},
Rafał Malinowski's avatar
Rafał Malinowski committed
178
		CurrentMessageRenderInfoFactory{nullptr},
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
		CurrentMessageTransformerService{nullptr},
		CurrentNotificationService{nullptr},
		CurrentFormattedStringFactory{nullptr},
		CurrentRawMessageTransformerService{nullptr},
		CurrentClipboardHtmlTransformerService{nullptr},
		CurrentAccountConfigurationWidgetFactoryRepository{nullptr},
		CurrentBuddyConfigurationWidgetFactoryRepository{nullptr},
		CurrentChatConfigurationWidgetFactoryRepository{nullptr},
		CurrentChatTopBarWidgetFactoryRepository{nullptr},
		CurrentUnreadMessageRepository{nullptr},
		CurrentRosterNotifier{nullptr},
		CurrentChatWidgetActions{nullptr},
		CurrentChatWidgetActivationService{nullptr},
		CurrentChatWidgetContainerHandlerMapper{nullptr},
		CurrentChatWidgetContainerHandlerRepository{nullptr},
		CurrentChatWidgetFactory{nullptr},
		CurrentChatWidgetManager{nullptr},
		CurrentChatWidgetMessageHandler{nullptr},
		CurrentChatWidgetRepository{nullptr},
		CurrentChatWindowFactory{nullptr},
		CurrentChatWindowManager{nullptr},
		CurrentChatWindowStorage{nullptr},
		CurrentChatWindowRepository{nullptr},
		CurrentStoragePointFactory{nullptr},
		CurrentPluginActivationService{nullptr},
		CurrentPluginActivationErrorHandler{nullptr},
		CurrentPluginConflictResolver{nullptr},
		CurrentPluginDependencyGraphBuilder{nullptr},
		CurrentPluginDependencyHandler{nullptr},
		CurrentPluginMetadataFinder{nullptr},
		CurrentPluginMetadataReader{nullptr},
		CurrentPluginStateManager{nullptr},
		CurrentPluginStateService{nullptr},
		CurrentPluginManager{nullptr},
213 214
		Window(0),
		Myself(Buddy::create()), IsClosing(false),
215
		ShowMainWindowOnStart(true)
Rafał Malinowski's avatar
Rafał Malinowski committed
216
{
217 218
	// must be created first
	CurrentStoragePointFactory = new StoragePointFactory(this);
219
	CurrentStoragePointFactory->setConfigurationFile(KaduApplication::instance()->configurationApi());
220 221
	Instance = this; // TODO: fix this hack

222
	connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit()));
223

224
	import_0_6_5_configuration();
225
	createDefaultConfiguration();
226
	configurationUpdated();
227

228
	MainConfigurationHolder::createInstance();
229

230 231 232
	Parser::GlobalVariables.insert(QLatin1String("DATA_PATH"), KaduPaths::instance()->dataPath());
	Parser::GlobalVariables.insert(QLatin1String("HOME"), KaduPaths::homePath());
	Parser::GlobalVariables.insert(QLatin1String("KADU_CONFIG"), KaduPaths::instance()->profilePath());
233
	DateTimeParserTags::registerParserTags();
234 235

	importPre10Configuration();
Rafał Malinowski's avatar
Rafał Malinowski committed
236 237 238 239
}

Core::~Core()
{
240
	IsClosing = true;
241

242
	CurrentPluginStateManager->storePluginStates();
243

244 245
	// unloading modules does that
	/*StatusContainerManager::instance()->disconnectAndStoreLastStatus(disconnectWithCurrentDescription, disconnectDescription);*/
246
	CurrentChatWindowManager->storeOpenedChatWindows();
247
	ConfigurationManager::instance()->flush();
248 249
// 	delete Configuration;
// 	Configuration = 0;
250

251
	KaduApplication::instance()->configurationApi()->makeBackup();
252

253
	CurrentPluginManager->deactivatePlugins();
Rafał Malinowski's avatar
Rafał Malinowski committed
254

255 256
	stopServices();

257
#ifdef Q_OS_MAC
258
	QApplication::setWindowIcon(KaduIcon("kadu_icons/kadu").icon());
259
#endif // Q_OS_MAC
260

261
	KaduWindowProvider->provideValue(0);
262
	QWidget *hiddenParent = Window->parentWidget();
263 264
	delete Window;
	Window = 0;
265
	delete hiddenParent;
266

267 268
	MainConfigurationHolder::destroyInstance();

Rafał Malinowski's avatar
Rafał Malinowski committed
269 270 271
	triggerAllAccountsUnregistered();
}

272 273
void Core::import_0_6_5_configuration()
{
274
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UserboxAlternateBgColor", KaduApplication::instance()->deprecatedConfigurationApi()->readEntry("Look", "UserboxBgColor"));
275 276
}

277 278
void Core::importPre10Configuration()
{
279
	if (KaduApplication::instance()->deprecatedConfigurationApi()->readBoolEntry("General", "ImportedPre10"))
280 281 282 283 284 285 286 287 288
	{
		return;
	}

	foreach (const Buddy &buddy, BuddyManager::instance()->items())
	{
		if (buddy.isNull() || buddy.isAnonymous())
			continue;

289
		bool notify = buddy.property("notify:Notify", false).toBool() || KaduApplication::instance()->deprecatedConfigurationApi()->readBoolEntry("Notify", "NotifyAboutAll");
290 291 292 293 294 295 296

		if (notify)
			buddy.removeProperty("notify:Notify");
		else
			buddy.addProperty("notify:Notify", false, CustomProperties::Storable);
	}

297
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "ImportedPre10", true);
298 299
}

Rafał Malinowski's avatar
Rafał Malinowski committed
300 301 302 303
void Core::createDefaultConfiguration()
{
	QWidget w;

304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "AutoSend", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "BlinkChatTitle", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ChatCloseTimer", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ChatCloseTimerPeriod", 2);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ChatPrune", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ChatPruneLen", 0);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ConfirmChatClear", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "IgnoreAnonymousRichtext", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "IgnoreAnonymousUsers", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "IgnoreAnonymousUsersInConferences", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "LastImagePath", QDir::homePath() + '/');
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "NewMessagesInChatTitle", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "OpenChatOnMessage", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "OpenChatOnMessageWhenOnline", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "SaveOpenedWindows", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ReceiveMessages", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "RecentChatsTimeout", 240);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "RecentChatsClear", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "RememberPosition", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ShowEditWindowLabel", true);

	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "AllowExecutingFromParser", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "CheckUpdates", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "DEBUG_MASK", KDEBUG_ALL & ~KDEBUG_FUNCTION_END);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "DescriptionHeight", 60);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "DisconnectWithCurrentDescription", true);
330
#ifdef Q_OS_WIN32
331
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "HideMainWindowFromTaskbar", false);
332
#endif
333 334 335 336 337 338 339 340 341 342 343 344
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "Language",  QLocale::system().name().left(2));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "Nick", tr("Me"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "NumberOfDescriptions", 20);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "ParseStatus", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "ShowBlocked", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "ShowBlocking", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "ShowMyself", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "ShowOffline", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "ShowOnlineAndDescription", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "ShowWithoutDescription", true);

	if (KaduApplication::instance()->deprecatedConfigurationApi()->readBoolEntry("General", "AdvancedMode", false))
345
	{
346 347
		KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "StatusContainerType", "Account");
		KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ShowExpandingControl", true);
348
	}
349
	else
350
	{
351 352
		KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "StatusContainerType", "Identity");
		KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ShowExpandingControl", false);
353
	}
354

355 356 357 358 359 360 361 362 363 364 365 366 367
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "StartupLastDescription", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "StartupStatus", "LastStatus");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "StartupStatusInvisibleWhenLastWasOffline", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "UserBoxHeight", 300);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "WindowActivationMethod", 0);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "MainConfiguration_Geometry", "50, 50, 790, 580");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("General", "LookChatAdvanced_Geometry", "50, 50, 620, 540");

	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "AlignUserboxIconsTop", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "AvatarBorder", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "AvatarGreyOut", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatContents", QString());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ForceCustomChatFont", false);
368
	QFont chatFont = qApp->font();
369
#ifdef Q_OS_WIN32
370 371 372 373 374 375 376
	// On Windows default app font is often "MS Shell Dlg 2", and the default sans
	// family (Arial, at least in Qt 4.8) is better. Though, on X11 the default
	// sans family is the same while most users will have some nice default app
	// font, like DejaVu, Ubuntu (the font, not the distro) or alike.
	chatFont.setStyleHint(QFont::SansSerif);
	chatFont.setFamily(chatFont.defaultFamily());
#endif
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatFont", chatFont);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatBgFilled", // depends on configuration imported from older version
		KaduApplication::instance()->deprecatedConfigurationApi()->readColorEntry("Look", "ChatBgColor").isValid() &&
		KaduApplication::instance()->deprecatedConfigurationApi()->readColorEntry("Look", "ChatBgColor") != QColor("#ffffff"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatBgColor", QColor("#ffffff"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatMyBgColor", QColor("#E0E0E0"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatMyFontColor", QColor("#000000"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatMyNickColor", QColor("#000000"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatUsrBgColor", QColor("#F0F0F0"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatUsrFontColor", QColor("#000000"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatUsrNickColor", QColor("#000000"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatTextCustomColors", // depends on configuration imported from older version
		KaduApplication::instance()->deprecatedConfigurationApi()->readColorEntry("Look", "ChatTextBgColor").isValid() &&
		KaduApplication::instance()->deprecatedConfigurationApi()->readColorEntry("Look", "ChatTextBgColor") != QColor("#ffffff"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatTextBgColor", QColor("#ffffff"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ChatTextFontColor", QColor("#000000"));
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ConferenceContents", QString());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ConferencePrefix", QString());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "DescriptionColor", w.palette().text().color());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "DisplayGroupTabs", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "HeaderSeparatorHeight", 1);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "InfoPanelFgColor", w.palette().text().color());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "InfoPanelBgFilled", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "InfoPanelBgColor", w.palette().base().color());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "InfoPanelSyntaxFile", "ultr");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "NiceDateFormat", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "NoHeaderInterval", 30);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "NoHeaderRepeat", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "NoServerTime", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "NoServerTimeDiff", 60);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "PanelFont", qApp->font());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "PanelVerticalScrollbar", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ParagraphSeparator", 4);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ShowAvatars", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "IconTheme", IconThemeManager::defaultTheme());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ShowGroupAll", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ShowBold", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ShowDesc", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ShowInfoPanel", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ShowMultilineDesc", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "ShowStatusButton", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "Style", "Satin");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UserboxBackgroundDisplayStyle", "Stretched");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UserboxTransparency", false);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UserboxAlpha", 0);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UserboxBlur", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UserboxBgColor", w.palette().base().color());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UserboxAlternateBgColor", w.palette().alternateBase().color());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UserBoxColumnCount", 1);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UserboxFgColor", w.palette().text().color());
Rafał Malinowski's avatar
Rafał Malinowski committed
427 428
	QFont userboxfont(qApp->font());
	userboxfont.setPointSize(qApp->font().pointSize() + 1);
429 430
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UserboxFont", userboxfont);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "UseUserboxBackground", false);
Rafał Malinowski's avatar
Rafał Malinowski committed
431 432 433 434
#ifdef Q_OS_MAC
	/* Dorr: for MacOS X define the icon notification to animated which
	 * will prevent from blinking the dock icon
	 */
435
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Look", "NewMessageIcon", 2);
Rafał Malinowski's avatar
Rafał Malinowski committed
436 437
#endif

438 439 440 441 442
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Network", "AllowDCC", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Network", "DefaultPort", 0);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Network", "isDefServers", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Network", "Server", QString());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Network", "UseProxy", false);
Rafał Malinowski's avatar
Rafał Malinowski committed
443 444 445 446

#ifdef Q_OS_MAC
	/* Dorr: for MacOS X define the function keys with 'apple' button
	 * as it is the default system configuration */
447 448 449 450 451
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "chat_clear", "Ctrl+F9");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_configure", "Ctrl+F2");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_modulesmanager", "Ctrl+F4");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_showoffline", "Ctrl+F9");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_showonlydesc", "Ctrl+F10");
Rafał Malinowski's avatar
Rafał Malinowski committed
452
#else
453 454 455 456 457
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "chat_clear", "F9");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_configure", "F2");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_modulesmanager", "F4");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_showoffline", "F9");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_showonlydesc", "F10");
Rafał Malinowski's avatar
Rafał Malinowski committed
458
#endif
459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "chat_bold", "Ctrl+B");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "chat_close", "Esc");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "chat_italic", "Ctrl+I");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "chat_underline", "Ctrl+U");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_adduser", "Ctrl+N");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_deleteuser", "Del");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_openchatwith", "Ctrl+L");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_persinfo", "Ins");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_searchuser", "Ctrl+F");
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("ShortCuts", "kadu_exit", "Ctrl+Q");

	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "UseDefaultWebBrowser", KaduApplication::instance()->deprecatedConfigurationApi()->readEntry("Chat", "WebBrowser").isEmpty());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "UseDefaultEMailClient", KaduApplication::instance()->deprecatedConfigurationApi()->readEntry("Chat", "MailClient").isEmpty());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ContactStateChats", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ContactStateWindowTitle", true);
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ContactStateWindowTitleSyntax", QString());
	KaduApplication::instance()->deprecatedConfigurationApi()->addVariable("Chat", "ContactStateWindowTitlePosition", 1);
476

Rafał Malinowski's avatar
Rafał Malinowski committed
477 478 479 480 481
	createAllDefaultToolbars();
}

void Core::createAllDefaultToolbars()
{
482
	// don't use getToolbarsConfigElement here, we have to be sure that this element don'e exists
483
	QDomElement toolbarsConfig = KaduApplication::instance()->configurationApi()->findElement(KaduApplication::instance()->configurationApi()->rootElement(), "Toolbars");
Rafał Malinowski's avatar
Rafał Malinowski committed
484 485 486 487

	if (!toolbarsConfig.isNull())
		return; // no need for defaults...

488
	toolbarsConfig = KaduApplication::instance()->configurationApi()->createElement(KaduApplication::instance()->configurationApi()->rootElement(), "Toolbars");
Rafał Malinowski's avatar
Rafał Malinowski committed
489 490 491

	KaduWindow::createDefaultToolbars(toolbarsConfig);
	ChatEditBox::createDefaultToolbars(toolbarsConfig);
492
	SearchWindow::createDefaultToolbars(toolbarsConfig);
Rafał Malinowski's avatar
Rafał Malinowski committed
493

494
	KaduApplication::instance()->configurationApi()->sync();
Rafał Malinowski's avatar
Rafał Malinowski committed
495 496
}

497 498
void Core::init()
{
499 500
	MessageRenderInfo::registerParserTags();

501 502
	runServices();

503
	// protocol modules should be loaded before gui
504
	// it fixes crash on loading pending messages from config, contacts import from 0.6.5, and maybe other issues
505
	{
506
		auto changeNotifierLock = ChangeNotifierLock{CurrentPluginStateService->changeNotifier()};
507 508
		CurrentPluginManager->activateProtocolPlugins();
	}
509

510
	Myself.setAnonymous(false);
511
	Myself.setDisplay(KaduApplication::instance()->deprecatedConfigurationApi()->readEntry("General", "Nick", tr("Me")));
512

513
	new Updates(this);
514

515 516
	QApplication::setWindowIcon(KaduIcon("kadu_icons/kadu").icon());
	connect(IconsManager::instance(), SIGNAL(themeChanged()), this, SLOT(updateIcon()));
517
	QTimer::singleShot(15000, this, SLOT(deleteOldConfigurationFiles()));
518

519 520
	// TODO: add some life-cycle management
	NotificationManager::instance();
Rafał Malinowski's avatar
Rafał Malinowski committed
521

522 523 524
	AccountManager::instance()->ensureLoaded();
	BuddyManager::instance()->ensureLoaded();
	ContactManager::instance()->ensureLoaded();
525 526
	// Without that UnreadMessageRepository is loaded while filtering buddies list for the first time.
	// It has to happen earlier because UnreadMessageRepository::loaded() might add buddies to the BuddyManager
527
	// which (the buddies) otherwise will not be taken into account by buddies list before its next update.
528
	CurrentUnreadMessageRepository->ensureLoaded();
529
	AvatarManager::instance(); // initialize that
530

531
#if WITH_LIBINDICATE_QT
532 533 534
	// Use a symbol from libindicate-qt so that it will not get dropped for example by --as-needed.
	(void)QIndicate::Server::defaultInstance();
#endif
Rafał Malinowski's avatar
Rafał Malinowski committed
535 536
}

537 538
void Core::initialized()
{
539
	StatusSetter::instance()->coreInitialized();
540 541
}

Rafał Malinowski's avatar
Rafał Malinowski committed
542 543 544 545
void Core::deleteOldConfigurationFiles()
{
	kdebugf();

546 547 548
	QDir oldConfigs(KaduPaths::instance()->profilePath(), "kadu-0.12.conf.xml.backup.*", QDir::Name, QDir::Files);
	if (oldConfigs.count() > 20)
		for (unsigned int i = 0, max = oldConfigs.count() - 20; i < max; ++i)
549
			QFile::remove(KaduPaths::instance()->profilePath() + oldConfigs[static_cast<int>(i)]);
550

551
	QDir oldConfigs2(KaduPaths::instance()->profilePath(), "kadu-0.6.6.conf.xml.backup.*", QDir::Name, QDir::Files);
Rafał Malinowski's avatar
Rafał Malinowski committed
552 553
	if (oldConfigs2.count() > 20)
		for (unsigned int i = 0, max = oldConfigs2.count() - 20; i < max; ++i)
554
			QFile::remove(KaduPaths::instance()->profilePath() + oldConfigs2[static_cast<int>(i)]);
Rafał Malinowski's avatar
Rafał Malinowski committed
555

556
	QDir oldBacktraces(KaduPaths::instance()->profilePath(), "kadu.backtrace.*", QDir::Name, QDir::Files);
Rafał Malinowski's avatar
Rafał Malinowski committed
557 558
	if (oldBacktraces.count() > 20)
		for (unsigned int i = 0, max = oldBacktraces.count() - 20; i < max; ++i)
559
			QFile::remove(KaduPaths::instance()->profilePath() + oldBacktraces[static_cast<int>(i)]);
Rafał Malinowski's avatar
Rafał Malinowski committed
560

561
	QDir oldDebugs(KaduPaths::instance()->profilePath(), "kadu.log.*", QDir::Name, QDir::Files);
562 563
	if (oldDebugs.count() > 20)
		for (unsigned int i = 0, max = oldDebugs.count() - 20; i < max; ++i)
564
			QFile::remove(KaduPaths::instance()->profilePath() + oldDebugs[static_cast<int>(i)]);
Rafał Malinowski's avatar
Rafał Malinowski committed
565 566 567 568

	kdebugf2();
}

569
void Core::updateIcon()
570
{
571 572 573
	if (isClosing())
		return;

574
	QApplication::setWindowIcon(KaduIcon("kadu_icons/kadu").icon());
575 576 577 578
}

void Core::kaduWindowDestroyed()
{
579
	KaduWindowProvider->provideValue(0);
580 581 582
	Window = 0;
}

583 584
void Core::accountRegistered(Account account)
{
585 586 587
	Protocol *protocol = account.protocolHandler();
	if (!protocol)
		return;
Rafał Malinowski's avatar
Rafał Malinowski committed
588

589 590 591
	connect(protocol, SIGNAL(connecting(Account)), this, SIGNAL(connecting()));
	connect(protocol, SIGNAL(connected(Account)), this, SIGNAL(connected()));
	connect(protocol, SIGNAL(disconnected(Account)), this, SIGNAL(disconnected()));
Rafał Malinowski's avatar
Rafał Malinowski committed
592 593
}

594
void Core::accountUnregistered(Account account)
Rafał Malinowski's avatar
Rafał Malinowski committed
595
{
596
	Protocol *protocol = account.protocolHandler();
Rafał Malinowski's avatar
Rafał Malinowski committed
597

598
	if (protocol)
599
		disconnect(protocol, 0, this, 0);
Rafał Malinowski's avatar
Rafał Malinowski committed
600
}
Rafał Malinowski's avatar
Rafał Malinowski committed
601

Rafał Malinowski's avatar
Rafał Malinowski committed
602 603 604 605 606
void Core::configurationUpdated()
{
#ifdef Q_OS_WIN
	QSettings settings("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",
		       QSettings::NativeFormat);
607
	if(KaduApplication::instance()->deprecatedConfigurationApi()->readBoolEntry("General", "RunOnStartup"))
Rafał Malinowski's avatar
Rafał Malinowski committed
608 609 610 611 612 613
		settings.setValue("Kadu",
				QDir::toNativeSeparators(QCoreApplication::applicationFilePath()));
	else
		settings.remove("Kadu");
#endif

614 615
	bool ok;
	int newMask = qgetenv("DEBUG_MASK").toInt(&ok);
616
	debug_mask = ok ? newMask : KaduApplication::instance()->deprecatedConfigurationApi()->readNumEntry("General", "DEBUG_MASK", KDEBUG_ALL & ~KDEBUG_FUNCTION_END);
617

618
	Myself.setDisplay(KaduApplication::instance()->deprecatedConfigurationApi()->readEntry("General", "Nick", tr("Me")));
Rafał Malinowski's avatar
Rafał Malinowski committed
619 620
}

Rafał Malinowski's avatar
Rafał Malinowski committed
621 622
void Core::createGui()
{
623
	Window = new KaduWindow();
624
	connect(Window, SIGNAL(destroyed()), this, SLOT(kaduWindowDestroyed()));
625
	KaduWindowProvider->provideValue(Window);
626

627 628
	// initialize file transfers
	FileTransferManager::instance();
629 630
}

631 632
void Core::runServices()
{
633
	CurrentBuddyDataWindowRepository = new BuddyDataWindowRepository(this);
634
	CurrentChatDataWindowRepository = new ChatDataWindowRepository(this);
635
	CurrentChatImageRequestService = new ChatImageRequestService(this);
636
	CurrentDomProcessorService = new DomProcessorService(this);
637
	CurrentImageStorageService = new ImageStorageService(this);
638
	CurrentMessageFilterService = new MessageFilterService(this);
639
	CurrentMessageHtmlRendererService = new MessageHtmlRendererService(this);
640
	CurrentMessageTransformerService = new MessageTransformerService(this);
641
	CurrentFormattedStringFactory = new FormattedStringFactory();
642
	CurrentRawMessageTransformerService = new RawMessageTransformerService(this);
643
	CurrentClipboardHtmlTransformerService = new ClipboardHtmlTransformerService(this);
644
	CurrentAccountConfigurationWidgetFactoryRepository = new AccountConfigurationWidgetFactoryRepository(this);
645
	CurrentBuddyConfigurationWidgetFactoryRepository = new BuddyConfigurationWidgetFactoryRepository(this);
646
	CurrentChatConfigurationWidgetFactoryRepository = new ChatConfigurationWidgetFactoryRepository(this);
647
	CurrentChatTopBarWidgetFactoryRepository = new ChatTopBarWidgetFactoryRepository(this);
648
	CurrentUnreadMessageRepository = new UnreadMessageRepository(this);
649

650 651 652 653
	CurrentRosterNotifier = new RosterNotifier(this);
	foreach (const auto &notifyEvent, CurrentRosterNotifier->notifyEvents())
		NotificationManager::instance()->registerNotifyEvent(notifyEvent);

654 655
	CurrentChatWidgetActions = new ChatWidgetActions(this);

656 657
	CurrentChatWidgetFactory = new ChatWidgetFactory(this);
	CurrentChatWidgetFactory->setFormattedStringFactory(CurrentFormattedStringFactory);
658 659 660

	CurrentChatWidgetRepository = new ChatWidgetRepository(this);

661
	CurrentChatWindowFactory = new ChatWindowFactory(this);
662 663
	CurrentChatWindowRepository = new ChatWindowRepository(this);

664
	auto windowChatWidgetContainerHandler = new WindowChatWidgetContainerHandler(this);
665
	windowChatWidgetContainerHandler->setChatWindowFactory(CurrentChatWindowFactory);
666 667 668 669 670
	windowChatWidgetContainerHandler->setChatWindowRepository(CurrentChatWindowRepository);

	CurrentChatWidgetContainerHandlerRepository = new ChatWidgetContainerHandlerRepository(this);
	CurrentChatWidgetContainerHandlerRepository->registerChatWidgetContainerHandler(windowChatWidgetContainerHandler);

671 672 673 674
	CurrentChatWidgetContainerHandlerMapper = new ChatWidgetContainerHandlerMapper(this);
	CurrentChatWidgetContainerHandlerMapper->setChatWidgetContainerHandlerRepository(CurrentChatWidgetContainerHandlerRepository);
	CurrentChatWidgetContainerHandlerMapper->setChatWidgetRepository(CurrentChatWidgetRepository);

675 676
	CurrentChatWidgetActivationService = new ChatWidgetActivationService(this);
	CurrentChatWidgetActivationService->setChatWidgetContainerHandlerMapper(CurrentChatWidgetContainerHandlerMapper);
677
	CurrentChatWidgetActivationService->setChatWidgetContainerHandlerRepository(CurrentChatWidgetContainerHandlerRepository);
678

679
	CurrentChatWidgetManager = new ChatWidgetManager(this);
680
	CurrentChatWidgetManager->setChatWidgetActivationService(CurrentChatWidgetActivationService);
681 682 683
	CurrentChatWidgetManager->setChatWidgetFactory(CurrentChatWidgetFactory);
	CurrentChatWidgetManager->setChatWidgetRepository(CurrentChatWidgetRepository);

684
	CurrentChatWidgetMessageHandler = new ChatWidgetMessageHandler(this);
685
	CurrentChatWidgetMessageHandler->setChatWidgetActivationService(CurrentChatWidgetActivationService);
686 687 688 689
	CurrentChatWidgetMessageHandler->setChatWidgetManager(CurrentChatWidgetManager);
	CurrentChatWidgetMessageHandler->setChatWidgetRepository(CurrentChatWidgetRepository);
	CurrentChatWidgetMessageHandler->setMessageManager(MessageManager::instance());
	CurrentChatWidgetMessageHandler->setUnreadMessageRepository(CurrentUnreadMessageRepository);
690 691
	auto chatWidgetMessageHandlerConfigurator = new ChatWidgetMessageHandlerConfigurator(); // this is basically a global so we do not care about relesing it
	chatWidgetMessageHandlerConfigurator->setChatWidgetMessageHandler(CurrentChatWidgetMessageHandler);
692

693 694
	CurrentChatWindowStorage = new ChatWindowStorage(this);
	CurrentChatWindowStorage->setChatManager(ChatManager::instance());
695
	CurrentChatWindowStorage->setStoragePointFactory(CurrentStoragePointFactory);
696 697 698
	auto chatWindowStorageConfigurator = new ChatWindowStorageConfigurator(); // this is basically a global so we do not care about relesing it
	chatWindowStorageConfigurator->setChatWindowStorage(CurrentChatWindowStorage);

699
	CurrentChatWindowManager = new ChatWindowManager(this);
700
	CurrentChatWindowManager->setChatWidgetManager(CurrentChatWidgetManager);
701 702
	CurrentChatWindowManager->setChatWindowRepository(CurrentChatWindowRepository);
	CurrentChatWindowManager->setChatWindowStorage(CurrentChatWindowStorage);
703

704 705 706 707
	// this instance lives forever
	// TODO: maybe make it QObject and make CurrentChatImageRequestService its parent
	ChatImageRequestServiceConfigurator *configurator = new ChatImageRequestServiceConfigurator();
	configurator->setChatImageRequestService(CurrentChatImageRequestService);
708

709 710 711
	CurrentChatImageRequestService->setImageStorageService(CurrentImageStorageService);
	CurrentChatImageRequestService->setAccountManager(AccountManager::instance());
	CurrentChatImageRequestService->setContactManager(ContactManager::instance());
712 713 714

	MessageManager::instance()->setMessageFilterService(CurrentMessageFilterService);
	MessageManager::instance()->setMessageTransformerService(CurrentMessageTransformerService);
715 716 717
	MessageManager::instance()->setFormattedStringFactory(CurrentFormattedStringFactory);

	CurrentFormattedStringFactory->setImageStorageService(CurrentImageStorageService);
718

719
	ChatStyleManager::instance()->setFormattedStringFactory(CurrentFormattedStringFactory);
720 721

	CurrentMessageHtmlRendererService->setDomProcessorService(CurrentDomProcessorService);
722
	CurrentMessageRenderInfoFactory = new MessageRenderInfoFactory();
723
	CurrentMessageRenderInfoFactory->setChatStyleManager(ChatStyleManager::instance());
724

725
	CurrentPluginActivationErrorHandler = new PluginActivationErrorHandler{this};
726 727
	CurrentPluginActivationService = new PluginActivationService(this);

728 729
	CurrentPluginMetadataFinder = new PluginMetadataFinder(this);
	CurrentPluginMetadataReader = new PluginMetadataReader(this);
730
	CurrentPluginStateManager = new PluginStateManager(this);
731
	CurrentPluginStateService = new PluginStateService(this);
732 733

	CurrentPluginDependencyGraphBuilder = new PluginDependencyGraphBuilder(this);
734

735
	CurrentPluginMetadataFinder->setDirectory(KaduPaths::instance()->dataPath() + QLatin1String{"plugins"});
736
	CurrentPluginMetadataFinder->setPluginMetadataReader(CurrentPluginMetadataReader);
737

738 739
	CurrentPluginDependencyHandler = new PluginDependencyHandler(this);
	CurrentPluginDependencyHandler->setPluginDependencyGraphBuilder(CurrentPluginDependencyGraphBuilder);
740
	CurrentPluginDependencyHandler->setPluginMetadataProvider(CurrentPluginMetadataFinder);
741

742 743 744
	CurrentPluginConflictResolver = new PluginConflictResolver{this};
	CurrentPluginConflictResolver->setPluginDependencyHandler(CurrentPluginDependencyHandler);

745 746 747 748
	CurrentPluginActivationService->setPluginActivationErrorHandler(CurrentPluginActivationErrorHandler);
	CurrentPluginActivationService->setPluginDependencyHandler(CurrentPluginDependencyHandler);
	CurrentPluginActivationService->setPluginStateService(CurrentPluginStateService);

749 750
	CurrentPluginManager = new PluginManager(this);
	CurrentPluginManager->setPluginActivationService(CurrentPluginActivationService);
751
	CurrentPluginManager->setPluginDependencyHandler(CurrentPluginDependencyHandler);
752
	CurrentPluginManager->setPluginStateService(CurrentPluginStateService);
753

754 755 756
	CurrentPluginActivationErrorHandler->setPluginActivationService(CurrentPluginActivationService);
	CurrentPluginActivationErrorHandler->setPluginStateService(CurrentPluginStateService);

757
	CurrentPluginStateManager->setPluginDependencyHandler(CurrentPluginDependencyHandler);
758 759 760
	CurrentPluginStateManager->setPluginStateService(CurrentPluginStateService);
	CurrentPluginStateManager->setStoragePointFactory(CurrentStoragePointFactory);

761
	CurrentPluginDependencyHandler->initialize();
762
	CurrentPluginStateManager->loadPluginStates();
763

764
	CurrentChatStyleRendererFactoryProvider = make_qobject<ConfiguredChatStyleRendererFactoryProvider>(this);
765

766
	ChatStyleManager::instance()->setConfiguredChatStyleRendererFactoryProvider(CurrentChatStyleRendererFactoryProvider.get());
767 768

	CurrentWebkitMessagesViewDisplayFactory = make_qobject<WebkitMessagesViewDisplayFactory>(this);
769
	CurrentWebkitMessagesViewDisplayFactory->setChatStyleManager(ChatStyleManager::instance());
770
	CurrentWebkitMessagesViewDisplayFactory->setMessageRenderInfoFactory(CurrentMessageRenderInfoFactory);
771 772 773 774 775 776 777 778 779

	CurrentWebkitMessagesViewHandlerFactory = make_qobject<WebkitMessagesViewHandlerFactory>(this);
	CurrentWebkitMessagesViewHandlerFactory->setChatStyleManager(ChatStyleManager::instance());
	CurrentWebkitMessagesViewHandlerFactory->setWebkitMessagesViewDisplayFactory(CurrentWebkitMessagesViewDisplayFactory.get());

	CurrentWebkitMessagesViewFactory = make_qobject<WebkitMessagesViewFactory>(this);
	CurrentWebkitMessagesViewFactory->setChatStyleRendererFactoryProvider(CurrentChatStyleRendererFactoryProvider.get());
	CurrentWebkitMessagesViewFactory->setImageStorageService(CurrentImageStorageService);
	CurrentWebkitMessagesViewFactory->setWebkitMessagesViewHandlerFactory(CurrentWebkitMessagesViewHandlerFactory.get());
780 781 782

	// moved here because of #2758
	ContactManager::instance()->init();
783 784
}

785 786 787
void Core::runGuiServices()
{
	CurrentNotificationService = new NotificationService(this);
788
	CurrentChatWidgetMessageHandler->setNotificationService(CurrentNotificationService);
789 790

	CurrentChatWindowManager->openStoredChatWindows();
791 792
}

793 794 795 796 797 798
void Core::stopServices()
{
	delete CurrentChatWidgetRepository;
	CurrentChatWidgetRepository = 0;
}

799 800
void Core::activatePlugins()
{
801
	auto changeNotifierLock = ChangeNotifierLock{CurrentPluginStateService->changeNotifier()};
802 803
	CurrentPluginManager->activatePlugins();
	CurrentPluginManager->activateReplacementPlugins();
804 805
}

806 807 808 809 810
BuddyDataWindowRepository * Core::buddyDataWindowRepository() const
{
	return CurrentBuddyDataWindowRepository;
}

811 812 813 814 815
ChatDataWindowRepository * Core::chatDataWindowRepository() const
{
	return CurrentChatDataWindowRepository;
}

816 817 818
ChatImageRequestService * Core::chatImageRequestService() const
{
	return CurrentChatImageRequestService;
819 820
}

821 822 823 824 825
DomProcessorService * Core::domProcessorService() const
{
	return CurrentDomProcessorService;
}

826 827 828
ImageStorageService * Core::imageStorageService() const
{
	return CurrentImageStorageService;
829 830
}

831 832 833 834 835
MessageFilterService * Core::messageFilterService() const
{
	return CurrentMessageFilterService;
}

836 837 838 839 840
MessageHtmlRendererService * Core::messageHtmlRendererService() const
{
	return CurrentMessageHtmlRendererService;
}

841 842 843 844 845
MessageRenderInfoFactory * Core::messageRenderInfoFactory() const
{
	return CurrentMessageRenderInfoFactory;
}

846 847 848 849 850
MessageTransformerService * Core::messageTransformerService() const
{
	return CurrentMessageTransformerService;
}

851 852 853 854 855
NotificationService * Core::notificationService() const
{
	return CurrentNotificationService;
}

856 857 858 859 860
FormattedStringFactory * Core::formattedStringFactory() const
{
	return CurrentFormattedStringFactory;
}

861 862 863 864 865
RawMessageTransformerService * Core::rawMessageTransformerService() const
{
	return CurrentRawMessageTransformerService;
}

866 867 868 869 870
ClipboardHtmlTransformerService * Core::clipboardHtmlTransformerService() const
{
	return CurrentClipboardHtmlTransformerService;
}

871 872 873 874 875
AccountConfigurationWidgetFactoryRepository * Core::accountConfigurationWidgetFactoryRepository() const
{
	return CurrentAccountConfigurationWidgetFactoryRepository;
}

876 877 878 879 880
BuddyConfigurationWidgetFactoryRepository * Core::buddyConfigurationWidgetFactoryRepository() const
{
	return CurrentBuddyConfigurationWidgetFactoryRepository;
}

881 882 883 884 885
ChatConfigurationWidgetFactoryRepository * Core::chatConfigurationWidgetFactoryRepository() const
{
	return CurrentChatConfigurationWidgetFactoryRepository;
}

886 887 888 889 890
ChatTopBarWidgetFactoryRepository * Core::chatTopBarWidgetFactoryRepository() const
{
	return CurrentChatTopBarWidgetFactoryRepository;
}

891 892 893 894 895
UnreadMessageRepository * Core::unreadMessageRepository() const
{
	return CurrentUnreadMessageRepository;
}

896 897 898 899 900
RosterNotifier * Core::rosterNotifier() const
{
	return CurrentRosterNotifier;
}

901 902 903 904 905
ChatWidgetContainerHandlerMapper * Core::chatWidgetContainerHandlerMapper() const
{
	return CurrentChatWidgetContainerHandlerMapper;
}

906 907 908 909 910
ChatWidgetContainerHandlerRepository * Core::chatWidgetContainerHandlerRepository() const
{
	return CurrentChatWidgetContainerHandlerRepository;
}

911 912 913 914 915
ChatWidgetActions * Core::chatWidgetActions() const
{
	return CurrentChatWidgetActions;
}

916 917 918 919 920
ChatWidgetActivationService * Core::chatWidgetActivationService() const
{
	return CurrentChatWidgetActivationService;
}

921 922 923 924 925
ChatWidgetManager * Core::chatWidgetManager() const
{
	return CurrentChatWidgetManager;
}

926 927 928 929 930
ChatWidgetFactory * Core::chatWidgetFactory() const
{
	return CurrentChatWidgetFactory;
}

931 932 933 934 935
ChatWidgetRepository * Core::chatWidgetRepository() const
{
	return CurrentChatWidgetRepository;
}

936 937 938 939 940
ChatWindowFactory * Core::chatWindowFactory() const
{
	return CurrentChatWindowFactory;
}

941 942 943 944 945
ChatWindowManager * Core::chatWindowManager() const
{
	return CurrentChatWindowManager;
}

946 947 948 949 950
ChatWindowRepository * Core::chatWindowRepository() const
{
	return CurrentChatWindowRepository;
}

951 952 953 954 955
StoragePointFactory * Core::storagePointFactory() const
{
	return CurrentStoragePointFactory;
}

956 957 958 959 960
PluginActivationErrorHandler * Core::pluginActivationErrorHandler() const
{
	return CurrentPluginActivationErrorHandler;
}

961 962 963 964 965
PluginActivationService * Core::pluginActivationService() const
{
	return CurrentPluginActivationService;
}

966 967 968 969 970
PluginConflictResolver * Core::pluginConflictResolver() const
{
	return CurrentPluginConflictResolver;
}

971 972 973 974 975
PluginDependencyGraphBuilder * Core::pluginDependencyGraphBuilder() const
{
	return CurrentPluginDependencyGraphBuilder;
}

976 977 978 979 980
PluginDependencyHandler * Core::pluginDependencyHandler() const
{
	return CurrentPluginDependencyHandler;
}

981
PluginMetadataProvider * Core::pluginMetadataProvider() const
982
{
983
	return CurrentPluginMetadataFinder;
984 985
}

986
PluginMetadataReader * Core::pluginMetadataReader() const
987
{
988
	return CurrentPluginMetadataReader;
989 990
}

991 992 993 994 995
PluginStateManager * Core::pluginStateManager() const
{
	return CurrentPluginStateManager;
}

996 997 998 999 1000
PluginStateService * Core::pluginStateService() const
{
	return CurrentPluginStateService;
}

1001
PluginManager * Core::pluginManager() const
1002
{
1003
	return CurrentPluginManager;
1004 1005
}

1006
ChatStyleRendererFactoryProvider * Core::chatStyleRendererFactoryProvider() const
1007
{
1008
	return CurrentChatStyleRendererFactoryProvider.get();
1009 1010
}

1011
ConfiguredChatStyleRendererFactoryProvider * Core::configuredChatStyleRendererFactoryProvider() const
1012
{
1013
	return CurrentChatStyleRendererFactoryProvider.get();
1014 1015
}

1016 1017 1018 1019 1020
WebkitMessagesViewDisplayFactory * Core::webkitMessagesViewDisplayFactory() const
{
	return CurrentWebkitMessagesViewDisplayFactory.get();
}

1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
WebkitMessagesViewFactory * Core::webkitMessagesViewFactory() const
{
	return CurrentWebkitMessagesViewFactory.get();
}

WebkitMessagesViewHandlerFactory * Core::webkitMessagesViewHandlerFactory() const
{
	return CurrentWebkitMessagesViewHandlerFactory.get();
}

1031 1032
void Core::showMainWindow()
{
1033
	if (ShowMainWindowOnStart)
1034
		MainWindowProvider->provide()->show();
1035

1036
	// after first call which has to be placed in main(), this method should always show main window
1037
	ShowMainWindowOnStart = true;
1038 1039 1040 1041 1042
}

void Core::setShowMainWindowOnStart(bool show)
{
	ShowMainWindowOnStart = show;
1043 1044 1045 1046 1047
}

KaduWindow * Core::kaduWindow()
{
	return Window;
Rafał Malinowski's avatar
Rafał Malinowski committed
1048
}
1049

1050
const std::shared_ptr<DefaultProvider<QWidget *>> & Core::mainWindowProvider() const
1051 1052 1053 1054
{
	return MainWindowProvider;
}

1055 1056 1057
void Core::receivedSignal(const QString &signal)
{
	if ("activate" == signal)
1058
		_activateWindow(MainWindowProvider->provide());
1059
	else
Bartosz Brachaczek's avatar
Bartosz Brachaczek committed
1060
		UrlHandlerManager::instance()->openUrl(signal.toUtf8(), true);
1061 1062
}

1063 1064 1065 1066 1067 1068 1069 1070
void Core::quit()
{
	if (!Instance)
		return;

	delete Instance;
	Instance = 0;
}
Bartosz Brachaczek's avatar
Bartosz Brachaczek committed
1071 1072

#include "moc_core.cpp"