File: ktikzapplication.cpp

package info (click to toggle)
ktikz 0.11~git20161122-1
  • links: PTS
  • area: main
  • in suites: stretch
  • size: 5,248 kB
  • ctags: 1,225
  • sloc: cpp: 10,302; xml: 701; sh: 150; makefile: 29
file content (194 lines) | stat: -rw-r--r-- 6,627 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/***************************************************************************
 *   Copyright (C) 2009, 2010, 2011, 2012 by Glad Deschrijver              *
 *     <glad.deschrijver@gmail.com>                                        *
 *                                                                         *
 *   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/>.  *
 ***************************************************************************/

#include "ktikzapplication.h"

#include "mainwindow.h"

#ifdef KTIKZ_USE_KDE
#include <KCmdLineArgs>
#include <KUrl>

KtikzApplication::KtikzApplication(int &argc, char **argv)
	: KApplication()
{
	Q_UNUSED(argc);
	Q_UNUSED(argv);
}

void KtikzApplication::init()
{
	if (isSessionRestored())
	{
		kRestoreMainWindows<MainWindow>();
		return;
	}

	MainWindow *mainWindow = new MainWindow;
	mainWindow->show();

	KUrl url;
	KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
	for (int i = 0; i < args->count(); ++i)
	{
		url = args->url(i);
		if (url.isValid() && url.isLocalFile())
			mainWindow->loadUrl(Url(url));
	}
	args->clear();
}

QString KtikzApplication::applicationName()
{
	return QLatin1String("KtikZ");
}
#else
#include <QtCore/QFileInfo>
#include <QtCore/QUrl>
#include <QtCore/QSettings>
#include <QtGui/QSessionManager>
#if QT_VERSION >= QT_VERSION_CHECK(5, 0, 0)
#include <QtWidgets/QMessageBox>
#else
#include <QtGui/QMessageBox>
#endif

KtikzApplication::KtikzApplication(int &argc, char **argv)
	: QApplication(argc, argv)
{
}

void KtikzApplication::init()
{
	if (isSessionRestored())
	{
		QSettings settings;
		settings.beginGroup(QLatin1String("Session") + sessionId());
		const int size = settings.beginReadArray(QLatin1String("MainWindowList"));
		for (int i = 0; i < size; ++i)
		{
			settings.setArrayIndex(i);
			const QString fileName = settings.value(QLatin1String("CurrentFile")).toString();
			const int lineNumber = settings.value(QLatin1String("LineNumber"), 1).toInt();
			MainWindow *mainWindow = new MainWindow;
			mainWindow->show();
			if (!fileName.isEmpty())
			{
				mainWindow->loadUrl(Url(fileName));
				mainWindow->setLineNumber(lineNumber);
			}
		}
		settings.endArray();
		settings.remove(QString());
		settings.endGroup();

		return;
	}

	MainWindow *mainWindow = new MainWindow;
	mainWindow->show();

	QStringList args = arguments();
	for (int i = 1; i < args.size(); ++i)
		mainWindow->loadUrl(Url(QFileInfo(args.at(i)).absoluteFilePath()));
}

QString KtikzApplication::applicationName()
{
	return QLatin1String("QtikZ");
}

void KtikzApplication::commitData(QSessionManager &manager)
{
	if (manager.allowsInteraction())
	{
		QList<int> saveDocuments;
		QList<MainWindow*> mainWindowList = MainWindow::mainWindowList();
		// find out which documents to save
		for (int i = 0; i < mainWindowList.size(); ++i)
		{
			if (mainWindowList.at(i)->isDocumentModified())
			{
				const QMessageBox::StandardButton ret = QMessageBox::warning(mainWindowList.at(i),
				                                     applicationName(),
				                                     tr("The document \"%1\" has been modified.\n"
				                                        "Do you want to save your changes?").arg(mainWindowList.at(i)->url().fileName()),
				                                     QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel,
				                                     QMessageBox::Save);
				if (ret == QMessageBox::Save)
					saveDocuments << i; // store the number of the document that has to be saved
				else if (ret == QMessageBox::Cancel)
					manager.cancel();
			}
		}
		manager.release(); // release the manager so that it can handle other programs
		// do the actual saving (after releasing the manager, so the manager doesn't have to wait until the saving has finished)
		for (int i = 0; i < saveDocuments.size(); ++i)
		{
			if (!mainWindowList.at(saveDocuments.at(i))->save())
				manager.cancel();
		}
	}
	else
	{
// TODO save unsaved documents to a temporary file
	}
}

void KtikzApplication::saveState(QSessionManager &manager)
{
	QList<MainWindow*> mainWindowList = MainWindow::mainWindowList();
	if (mainWindowList.size() == 0)
		return;

	// in X11 the session manager calls saveState() also on startup,
	// we don't want to save anything at startup, so we define a
	// discard command (in main.cpp) which cleans up the unnecessary
	// session information
	QStringList discard = QStringList(applicationFilePath());
	discard << QLatin1String("--discard") << sessionId();
	manager.setDiscardCommand(discard); // this lets xsm hang, but it works in real session management in KDE Plasma

	QSettings settings;
	settings.beginGroup(QLatin1String("Session") + sessionId());
	settings.beginWriteArray(QLatin1String("MainWindowList"));
	for (int i = 0; i < mainWindowList.size(); ++i)
	{
		settings.setArrayIndex(i);
		settings.setValue(QLatin1String("CurrentFile"), mainWindowList.at(i)->url().path());
		settings.setValue(QLatin1String("LineNumber"), mainWindowList.at(i)->lineNumber());
	}
	settings.endArray();
	settings.endGroup();
}
#endif

KtikzApplication::~KtikzApplication()
{
	// don't delete the MainWindows here because they are already deleted on
	// close, however:
	// the MainWindows are not deleted by the session manager when the session
	// closes, therefore we explicitly delete them here so that the temporary
	// files generated by them get deleted, note that we cannot delete them
	// in commitData() or saveState() because both need access to them and it
	// is not specified which is executed first, furthermore I see no other
	// way of deleting them when KTIKZ_USE_KDE is defined
	QList<MainWindow*> mainWindowList = MainWindow::mainWindowList();
	while (!mainWindowList.isEmpty())
		delete mainWindowList.takeFirst();
}