diff --git a/main.cpp b/main.cpp index c04b27514..35f1861b9 100644 --- a/main.cpp +++ b/main.cpp @@ -26,6 +26,8 @@ main(int argc, char *argv[]) ColorScheme::getInstance().setColors(0, -0.8); + Windows::load(); + MainWindow &w = Windows::getMainWindow(); w.show(); @@ -35,5 +37,7 @@ main(int argc, char *argv[]) Settings::getInstance().save(); + Windows::save(); + return ret; } diff --git a/widgets/mainwindow.cpp b/widgets/mainwindow.cpp index 1d2b2c823..eb0fe5525 100644 --- a/widgets/mainwindow.cpp +++ b/widgets/mainwindow.cpp @@ -3,7 +3,9 @@ #include "widgets/chatwidget.h" #include "widgets/notebook.h" +#include #include +#include namespace chatterino { namespace widgets { @@ -68,5 +70,34 @@ MainWindow::repaintVisibleChatWidgets(Channel *channel) } } } + +void +MainWindow::load(const boost::property_tree::ptree &tree) +{ + this->notebook.load(tree); + + this->loaded = true; } + +boost::property_tree::ptree +MainWindow::save() +{ + boost::property_tree::ptree child; + + child.put("type", "main"); + + this->notebook.save(child); + + return child; } + +void +MainWindow::loadDefaults() +{ + this->notebook.loadDefaults(); + + this->loaded = true; +} + +} // namespace widgets +} // namespace chatterino diff --git a/widgets/mainwindow.h b/widgets/mainwindow.h index 33b125286..560dd4a71 100644 --- a/widgets/mainwindow.h +++ b/widgets/mainwindow.h @@ -4,6 +4,7 @@ #include "widgets/notebook.h" #include +#include namespace chatterino { namespace widgets { @@ -19,8 +20,22 @@ public: void layoutVisibleChatWidgets(Channel *channel = NULL); void repaintVisibleChatWidgets(Channel *channel = NULL); + + void load(const boost::property_tree::ptree &tree); + boost::property_tree::ptree save(); + void loadDefaults(); + + bool + isLoaded() const + { + return this->loaded; + } + +private: + bool loaded = false; }; -} -} + +} // namespace widgets +} // namespace chatterino #endif // MAINWINDOW_H diff --git a/widgets/notebook.cpp b/widgets/notebook.cpp index ea00a75ca..10fe76843 100644 --- a/widgets/notebook.cpp +++ b/widgets/notebook.cpp @@ -5,10 +5,14 @@ #include "widgets/notebooktab.h" #include "widgets/settingsdialog.h" +#include +#include #include #include #include +#include #include +#include namespace chatterino { namespace widgets { @@ -35,8 +39,6 @@ Notebook::Notebook(QWidget *parent) this->userButton.icon = NotebookButton::IconUser; this->addButton.resize(24, 24); - - this->addPage(); } NotebookPage * @@ -191,5 +193,50 @@ Notebook::addPageButtonClicked() { addPage(true); } + +void +Notebook::load(const boost::property_tree::ptree &tree) +{ + // Read a list of tabs + try { + BOOST_FOREACH (const boost::property_tree::ptree::value_type &v, + tree.get_child("tabs.")) { + bool select = v.second.get("selected", false); + + auto page = this->addPage(select); + auto tab = page->tab; + tab->load(v.second); + page->load(v.second); + } + } catch (boost::property_tree::ptree_error &) { + // can't read tabs + } + + if (this->pages.size() == 0) { + // No pages saved, show default stuff + this->loadDefaults(); + } } + +void +Notebook::save(boost::property_tree::ptree &tree) +{ + boost::property_tree::ptree tabs; + + // Iterate through all tabs and add them to our tabs property thing + for (const auto &page : this->pages) { + boost::property_tree::ptree pTab = page->tab->save(); + tabs.push_back(std::make_pair("", pTab)); + } + + tree.add_child("tabs", tabs); } + +void +Notebook::loadDefaults() +{ + this->addPage(); +} + +} // namespace widgets +} // namespace chatterino diff --git a/widgets/notebook.h b/widgets/notebook.h index 919ed0d60..d5ef07fbb 100644 --- a/widgets/notebook.h +++ b/widgets/notebook.h @@ -7,6 +7,7 @@ #include #include +#include namespace chatterino { namespace widgets { @@ -54,8 +55,14 @@ private: NotebookButton userButton; NotebookPage *selectedPage; + +public: + void load(const boost::property_tree::ptree &tree); + void save(boost::property_tree::ptree &tree); + void loadDefaults(); }; -} -} + +} // namespace widgets +} // namespace chatterino #endif // NOTEBOOK_H diff --git a/widgets/notebookpage.cpp b/widgets/notebookpage.cpp index cba986f35..d2e1582b7 100644 --- a/widgets/notebookpage.cpp +++ b/widgets/notebookpage.cpp @@ -3,11 +3,13 @@ #include "widgets/chatwidget.h" #include "widgets/notebooktab.h" +#include #include #include #include #include #include +#include namespace chatterino { namespace widgets { @@ -73,9 +75,8 @@ NotebookPage::removeFromLayout(ChatWidget *widget) } void -NotebookPage::addToLayout( - ChatWidget *widget, - std::pair position = std::pair(-1, -1)) +NotebookPage::addToLayout(ChatWidget *widget, std::pair position = + std::pair(-1, -1)) { this->chatWidgets.push_back(widget); @@ -239,5 +240,14 @@ NotebookPage::paintEvent(QPaintEvent *) ColorScheme::getInstance().TabSelectedBackground); } } + +void +NotebookPage::load(const boost::property_tree::ptree &v) +{ + const std::string &tabName = v.get("name", "UNNAMED"); + + qDebug() << "tab name :" << tabName.c_str(); } -} + +} // namespace widgets +} // namespace chatterino diff --git a/widgets/notebookpage.h b/widgets/notebookpage.h index d13b78286..926635233 100644 --- a/widgets/notebookpage.h +++ b/widgets/notebookpage.h @@ -12,6 +12,7 @@ #include #include #include +#include namespace chatterino { namespace widgets { @@ -70,8 +71,12 @@ protected: private: void setPreviewRect(QPoint mousePos); + +public: + void load(const boost::property_tree::ptree &v); }; -} -} + +} // namespace widgets +} // namespace chatterino #endif // NOTEBOOKPAGE_H diff --git a/widgets/notebooktab.cpp b/widgets/notebooktab.cpp index 592a933fd..b4a568755 100644 --- a/widgets/notebooktab.cpp +++ b/widgets/notebooktab.cpp @@ -203,5 +203,26 @@ NotebookTab::mouseMoveEvent(QMouseEvent *event) } } } + +void +NotebookTab::load(const boost::property_tree::ptree &tree) +{ + // Load tab text + try { + this->setText(QString::fromStdString(tree.get("text"))); + } catch (boost::property_tree::ptree_error) { + } } + +boost::property_tree::ptree +NotebookTab::save() +{ + boost::property_tree::ptree tree; + + tree.put("text", this->getText().toStdString()); + + return tree; } + +} // namespace widgets +} // namespace chatterino diff --git a/widgets/notebooktab.h b/widgets/notebooktab.h index db080f61c..51a8caf35 100644 --- a/widgets/notebooktab.h +++ b/widgets/notebooktab.h @@ -3,6 +3,7 @@ #include #include +#include namespace chatterino { namespace widgets { @@ -117,8 +118,13 @@ private slots: calcSize(); update(); } + +public: + void load(const boost::property_tree::ptree &tree); + boost::property_tree::ptree save(); }; -} -} + +} // namespace widgets +} // namespace chatterino #endif // NOTEBOOKTAB_H diff --git a/windows.cpp b/windows.cpp index ca724bc90..518cb124a 100644 --- a/windows.cpp +++ b/windows.cpp @@ -1,7 +1,23 @@ #include "windows.h" +#include +#include +#include +#include + namespace chatterino { +static const std::string & +getSettingsPath() +{ + static std::string path = + (QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + + "/windows.json") + .toStdString(); + + return path; +} + QMutex Windows::windowMutex; widgets::MainWindow *Windows::mainWindow(nullptr); @@ -23,12 +39,67 @@ Windows::repaintVisibleChatWidgets(Channel *channel) } void -Windows::save() +Windows::load() { + 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("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 } void -Windows::load() +Windows::save() { + 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); } -} + +} // namespace chatterino diff --git a/windows.h b/windows.h index 196d91037..c13fca399 100644 --- a/windows.h +++ b/windows.h @@ -37,6 +37,7 @@ private: static widgets::MainWindow *mainWindow; }; -} + +} // namespace chatterino #endif // WINDOWS_H