2017-06-11 09:31:45 +02:00
|
|
|
#include "windowmanager.hpp"
|
|
|
|
#include "appdatapath.hpp"
|
2017-06-13 21:13:58 +02:00
|
|
|
#include "channelmanager.hpp"
|
2017-06-26 16:41:20 +02:00
|
|
|
#include "colorscheme.hpp"
|
2017-12-17 03:46:12 +01:00
|
|
|
#include "settingsmanager.hpp"
|
2017-01-15 16:38:30 +01:00
|
|
|
|
2017-01-28 22:35:23 +01:00
|
|
|
#include <QDebug>
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#include <boost/foreach.hpp>
|
|
|
|
#include <boost/property_tree/json_parser.hpp>
|
|
|
|
|
2017-01-18 21:30:23 +01:00
|
|
|
namespace chatterino {
|
2017-11-12 17:21:50 +01:00
|
|
|
WindowManager *WindowManager::instance = nullptr;
|
2017-04-13 19:25:33 +02:00
|
|
|
|
2017-12-17 02:18:13 +01:00
|
|
|
WindowManager::WindowManager(ChannelManager &_channelManager, ColorScheme &_colorScheme)
|
2017-06-13 21:13:58 +02:00
|
|
|
: channelManager(_channelManager)
|
2017-06-26 16:41:20 +02:00
|
|
|
, colorScheme(_colorScheme)
|
2017-04-13 19:25:33 +02:00
|
|
|
{
|
2017-11-12 17:21:50 +01:00
|
|
|
WindowManager::instance = this;
|
2017-04-13 19:25:33 +02:00
|
|
|
}
|
2017-01-18 21:30:23 +01:00
|
|
|
|
2017-12-14 00:25:06 +01:00
|
|
|
void WindowManager::initMainWindow()
|
|
|
|
{
|
|
|
|
this->selectedWindow = this->mainWindow =
|
2017-12-17 17:09:50 +01:00
|
|
|
new widgets::Window("main", this->channelManager, this->colorScheme, true);
|
2017-12-14 00:25:06 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
static const std::string &getSettingsPath()
|
2017-01-28 22:35:23 +01:00
|
|
|
{
|
2017-04-13 18:51:46 +02:00
|
|
|
static std::string path = (Path::getAppdataPath() + "uilayout.json").toStdString();
|
2017-01-28 22:35:23 +01:00
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void WindowManager::layoutVisibleChatWidgets(Channel *channel)
|
2017-01-15 16:38:30 +01:00
|
|
|
{
|
2017-09-21 17:34:41 +02:00
|
|
|
this->layout();
|
2017-01-16 03:15:07 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void WindowManager::repaintVisibleChatWidgets(Channel *channel)
|
2017-01-16 03:15:07 +01:00
|
|
|
{
|
2017-06-13 21:13:58 +02:00
|
|
|
if (this->mainWindow != nullptr) {
|
|
|
|
this->mainWindow->repaintVisibleChatWidgets(channel);
|
2017-01-26 04:26:40 +01:00
|
|
|
}
|
2017-01-15 16:38:30 +01:00
|
|
|
}
|
2017-01-26 21:04:01 +01:00
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void WindowManager::repaintGifEmotes()
|
2017-02-07 00:03:15 +01:00
|
|
|
{
|
2017-09-17 02:13:57 +02:00
|
|
|
this->repaintGifs();
|
2017-02-07 00:03:15 +01:00
|
|
|
}
|
|
|
|
|
2017-09-16 00:05:06 +02:00
|
|
|
// void WindowManager::updateAll()
|
|
|
|
//{
|
|
|
|
// if (this->mainWindow != nullptr) {
|
|
|
|
// this->mainWindow->update();
|
|
|
|
// }
|
|
|
|
//}
|
2017-02-02 01:23:26 +01:00
|
|
|
|
2017-11-12 17:21:50 +01:00
|
|
|
widgets::Window &WindowManager::getMainWindow()
|
2017-04-13 19:25:33 +02:00
|
|
|
{
|
2017-06-13 21:13:58 +02:00
|
|
|
return *this->mainWindow;
|
2017-04-13 19:25:33 +02:00
|
|
|
}
|
|
|
|
|
2017-12-14 00:25:06 +01:00
|
|
|
widgets::Window &WindowManager::getSelectedWindow()
|
2017-11-12 17:21:50 +01:00
|
|
|
{
|
2017-12-14 00:25:06 +01:00
|
|
|
return *this->selectedWindow;
|
2017-11-12 17:21:50 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
widgets::Window &WindowManager::createWindow()
|
|
|
|
{
|
2017-12-17 17:09:50 +01:00
|
|
|
auto *window = new widgets::Window("external", this->channelManager, this->colorScheme, false);
|
2017-11-12 17:21:50 +01:00
|
|
|
|
|
|
|
window->loadDefaults();
|
|
|
|
|
|
|
|
this->windows.push_back(window);
|
|
|
|
|
|
|
|
return *window;
|
|
|
|
}
|
|
|
|
|
2017-12-14 00:25:06 +01:00
|
|
|
int WindowManager::windowCount()
|
|
|
|
{
|
|
|
|
return this->windows.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
widgets::Window *WindowManager::windowAt(int index)
|
|
|
|
{
|
|
|
|
if (index < 0 || index >= this->windows.size()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
qDebug() << "getting window at bad index" << index;
|
|
|
|
|
|
|
|
return this->windows.at(index);
|
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void WindowManager::load()
|
2017-01-26 21:04:01 +01:00
|
|
|
{
|
2017-01-28 22:35:23 +01:00
|
|
|
const auto &settingsPath = getSettingsPath();
|
|
|
|
boost::property_tree::ptree tree;
|
|
|
|
|
|
|
|
try {
|
|
|
|
boost::property_tree::read_json(settingsPath, tree);
|
|
|
|
} catch (const boost::property_tree::json_parser_error &ex) {
|
2017-04-12 17:46:44 +02:00
|
|
|
qDebug() << "Error using property_tree::readJson: " << QString::fromStdString(ex.message());
|
2017-01-28 22:35:23 +01:00
|
|
|
|
2017-04-13 19:25:33 +02:00
|
|
|
getMainWindow().loadDefaults();
|
2017-01-28 22:35:23 +01:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Read a list of windows
|
|
|
|
try {
|
|
|
|
BOOST_FOREACH (const boost::property_tree::ptree::value_type &v,
|
|
|
|
tree.get_child("windows.")) {
|
|
|
|
qDebug() << QString::fromStdString(v.first.data());
|
|
|
|
const auto &type = v.second.get<std::string>("type", "unknown");
|
|
|
|
|
|
|
|
if (type == "main") {
|
2017-04-13 19:25:33 +02:00
|
|
|
getMainWindow().load(v.second);
|
2017-01-28 22:35:23 +01:00
|
|
|
} else {
|
|
|
|
qDebug() << "Unhandled window type: " << type.c_str();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (boost::property_tree::ptree_error &) {
|
|
|
|
// can't read windows
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the main window was not loaded properly, load defaults
|
2017-04-13 19:25:33 +02:00
|
|
|
if (!getMainWindow().isLoaded()) {
|
|
|
|
getMainWindow().loadDefaults();
|
2017-01-28 22:35:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no windows, create a default main window
|
2017-01-26 21:04:01 +01:00
|
|
|
}
|
|
|
|
|
2017-04-12 17:46:44 +02:00
|
|
|
void WindowManager::save()
|
2017-01-26 21:04:01 +01:00
|
|
|
{
|
2017-04-14 14:08:57 +02:00
|
|
|
auto &settingsPath = getSettingsPath();
|
2017-01-28 22:35:23 +01:00
|
|
|
boost::property_tree::ptree tree;
|
|
|
|
|
|
|
|
// Create windows array
|
|
|
|
boost::property_tree::ptree windows;
|
|
|
|
|
|
|
|
{
|
|
|
|
// save main window
|
2017-04-13 19:25:33 +02:00
|
|
|
auto child = getMainWindow().save();
|
2017-01-28 22:35:23 +01:00
|
|
|
windows.push_back(std::make_pair("", child));
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: iterate through rest of windows and add them to the "windows" ptree
|
|
|
|
|
|
|
|
tree.add_child("windows", windows);
|
|
|
|
|
|
|
|
boost::property_tree::write_json(settingsPath, tree);
|
2017-01-26 21:04:01 +01:00
|
|
|
}
|
2017-01-28 22:35:23 +01:00
|
|
|
|
2017-04-14 17:52:22 +02:00
|
|
|
} // namespace chatterino
|