2017-01-15 16:38:30 +01:00
|
|
|
#include "windows.h"
|
|
|
|
|
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-01-28 22:35:23 +01:00
|
|
|
static const std::string &
|
|
|
|
getSettingsPath()
|
|
|
|
{
|
|
|
|
static std::string path =
|
|
|
|
(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) +
|
|
|
|
"/windows.json")
|
|
|
|
.toStdString();
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2017-01-18 04:33:30 +01:00
|
|
|
QMutex Windows::windowMutex;
|
2017-01-15 16:38:30 +01:00
|
|
|
|
2017-01-26 04:26:40 +01:00
|
|
|
widgets::MainWindow *Windows::mainWindow(nullptr);
|
2017-01-15 16:38:30 +01:00
|
|
|
|
|
|
|
void
|
2017-01-16 15:06:12 +01:00
|
|
|
Windows::layoutVisibleChatWidgets(Channel *channel)
|
2017-01-15 16:38:30 +01:00
|
|
|
{
|
2017-01-26 04:26:40 +01:00
|
|
|
if (Windows::mainWindow != nullptr) {
|
|
|
|
Windows::mainWindow->layoutVisibleChatWidgets(channel);
|
|
|
|
}
|
2017-01-16 03:15:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-16 15:06:12 +01:00
|
|
|
Windows::repaintVisibleChatWidgets(Channel *channel)
|
2017-01-16 03:15:07 +01:00
|
|
|
{
|
2017-01-26 04:26:40 +01:00
|
|
|
if (Windows::mainWindow != nullptr) {
|
|
|
|
Windows::mainWindow->repaintVisibleChatWidgets(channel);
|
|
|
|
}
|
2017-01-15 16:38:30 +01:00
|
|
|
}
|
2017-01-26 21:04:01 +01:00
|
|
|
|
2017-02-07 00:03:15 +01:00
|
|
|
void
|
|
|
|
Windows::repaintGifEmotes()
|
|
|
|
{
|
|
|
|
if (Windows::mainWindow != nullptr) {
|
|
|
|
Windows::mainWindow->repaintGifEmotes();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-02 01:23:26 +01:00
|
|
|
void
|
|
|
|
Windows::updateAll()
|
|
|
|
{
|
|
|
|
if (Windows::mainWindow != nullptr) {
|
|
|
|
Windows::mainWindow->update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-26 21:04:01 +01:00
|
|
|
void
|
2017-01-28 22:35:23 +01:00
|
|
|
Windows::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) {
|
|
|
|
qDebug() << "Error using property_tree::readJson: "
|
|
|
|
<< QString::fromStdString(ex.message());
|
|
|
|
|
|
|
|
Windows::getMainWindow().loadDefaults();
|
|
|
|
|
|
|
|
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") {
|
|
|
|
Windows::getMainWindow().load(v.second);
|
|
|
|
} 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
|
|
|
|
if (!Windows::getMainWindow().isLoaded()) {
|
|
|
|
Windows::getMainWindow().loadDefaults();
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there are no windows, create a default main window
|
2017-01-26 21:04:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-01-28 22:35:23 +01:00
|
|
|
Windows::save()
|
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;
|
|
|
|
|
|
|
|
// Create windows array
|
|
|
|
boost::property_tree::ptree windows;
|
|
|
|
|
|
|
|
{
|
|
|
|
// save main window
|
|
|
|
auto child = Windows::getMainWindow().save();
|
|
|
|
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
|
|
|
|
|
|
|
} // namespace chatterino
|