1 /*
2  *    Copyright 2012, 2013 Thomas Schöps, Kai Pastor
3  *
4  *    This file is part of OpenOrienteering.
5  *
6  *    OpenOrienteering 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 3 of the License, or
9  *    (at your option) any later version.
10  *
11  *    OpenOrienteering 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 OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 
21 #include "home_screen_controller.h"
22 
23 #include <QFile>
24 #include <QStatusBar>
25 
26 #include "settings.h"
27 #include "gui/main_window.h"
28 #include "gui/widgets/home_screen_widget.h"
29 
30 
31 namespace OpenOrienteering {
32 
HomeScreenController()33 HomeScreenController::HomeScreenController()
34 : widget(nullptr)
35 , current_tip(-1)
36 {
37 	// nothing
38 }
39 
40 HomeScreenController::~HomeScreenController() = default;
41 
42 
statusBarVisible()43 bool HomeScreenController::statusBarVisible()
44 {
45 	return false;
46 }
47 
attach(MainWindow * window)48 void HomeScreenController::attach(MainWindow* window)
49 {
50 	this->window = window;
51 
52 	if (Settings::mobileModeEnforced())
53 	{
54 		widget = new HomeScreenWidgetMobile(this);
55 	}
56 	else
57 	{
58 		widget = new HomeScreenWidgetDesktop(this);
59 	}
60 
61 	window->setCentralWidget(widget);
62 
63 	connect(&Settings::getInstance(), &Settings::settingsChanged, this, &HomeScreenController::readSettings);
64 
65 	readSettings();
66 }
67 
detach()68 void HomeScreenController::detach()
69 {
70 	window->setCentralWidget(nullptr);
71 	widget->deleteLater();
72 
73 	Settings::getInstance().setSetting(Settings::HomeScreen_CurrentTip, current_tip);
74 }
75 
readSettings()76 void HomeScreenController::readSettings()
77 {
78 	Settings& settings = Settings::getInstance(); // FIXME: settings should be const
79 
80 	widget->setRecentFiles(settings.getSettingCached(Settings::General_RecentFilesList).toStringList());
81 	widget->setOpenMRUFileChecked(settings.getSettingCached(Settings::General_OpenMRUFile).toBool());
82 
83 	bool tips_visible = settings.getSettingCached(Settings::HomeScreen_TipsVisible).toBool();
84 	widget->setTipsVisible(tips_visible);
85 	if (tips_visible)
86 	{
87 		if (current_tip < 0)
88 		{
89 			current_tip = settings.getSettingCached(Settings::HomeScreen_CurrentTip).toInt();
90 			goToNextTip();
91 		}
92 		else
93 		{
94 			// Settings changed.
95 			goToTip(current_tip);
96 		}
97 	}
98 }
99 
setOpenMRUFile(bool state)100 void HomeScreenController::setOpenMRUFile(bool state)
101 {
102 	Settings::getInstance().setSetting(Settings::General_OpenMRUFile, state);
103 }
104 
clearRecentFiles()105 void HomeScreenController::clearRecentFiles()
106 {
107 	Settings::getInstance().remove(Settings::General_RecentFilesList);
108 }
109 
setTipsVisible(bool state)110 void HomeScreenController::setTipsVisible(bool state)
111 {
112 	Settings::getInstance().setSetting(Settings::HomeScreen_TipsVisible, state);
113 }
114 
goToNextTip()115 void HomeScreenController::goToNextTip()
116 {
117 	goToTip(current_tip + 1);
118 }
119 
goToPreviousTip()120 void HomeScreenController::goToPreviousTip()
121 {
122 	goToTip(current_tip - 1);
123 }
124 
goToTip(int index)125 void HomeScreenController::goToTip(int index)
126 {
127 	static QStringList tips;
128 	if (tips.isEmpty())
129 	{
130 		// Normally, this will be read only once.
131 		QFile file(QString::fromLatin1("doc:tip-of-the-day/tips.txt"));
132 		if (file.open(QIODevice::ReadOnly))
133 		{
134 			while (!file.atEnd())
135 			{
136 				QString tip(QString::fromUtf8(file.readLine().constData()));
137 				if (tip.endsWith(QLatin1Char('\n')))
138 					tip.chop(1);
139 				if (!tip.isEmpty())
140 					tips.push_back(tip);
141 			}
142 		}
143 	}
144 
145 	if (tips.isEmpty())
146 	{
147 		// Some error may have occurred during reading the tips file.
148 		// Display a welcome text.
149 		widget->setTipOfTheDay(QString::fromLatin1("<h2>%1</h2>").arg(tr("Welcome to OpenOrienteering Mapper!")));
150 	}
151 	else
152 	{
153 		Q_ASSERT(tips.count() > 0);
154 		while (index < 0)
155 			index += tips.count();
156 		current_tip = index % tips.count();
157 		widget->setTipOfTheDay(tips[current_tip]);
158 	}
159 }
160 
161 
162 }  // namespace OpenOrienteering
163