Changeset View
Changeset View
Standalone View
Standalone View
kwrite/main.cpp
Show All 13 Lines | 1 | /* This file is part of the KDE project | |||
---|---|---|---|---|---|
14 | 14 | | |||
15 | You should have received a copy of the GNU Library General Public License | 15 | You should have received a copy of the GNU Library General Public License | ||
16 | along with this library; see the file COPYING.LIB. If not, write to | 16 | along with this library; see the file COPYING.LIB. If not, write to | ||
17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | 17 | the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | ||
18 | Boston, MA 02110-1301, USA. | 18 | Boston, MA 02110-1301, USA. | ||
19 | */ | 19 | */ | ||
20 | 20 | | |||
21 | #include "kwrite.h" | 21 | #include "kwrite.h" | ||
22 | #include "kwriteapplication.h" | ||||
22 | 23 | | |||
23 | #include <ktexteditor/document.h> | 24 | #include <ktexteditor/document.h> | ||
24 | #include <ktexteditor/view.h> | 25 | #include <ktexteditor/view.h> | ||
25 | #include <ktexteditor/editor.h> | 26 | #include <ktexteditor/editor.h> | ||
26 | 27 | | |||
27 | #include <KAboutData> | 28 | #include <KAboutData> | ||
28 | #include <kcoreaddons_version.h> // for KAboutData::setDesktopFileName() | 29 | #include <kcoreaddons_version.h> // for KAboutData::setDesktopFileName() | ||
29 | #include <KLocalizedString> | 30 | #include <KLocalizedString> | ||
▲ Show 20 Lines • Show All 159 Lines • ▼ Show 20 Line(s) | 109 | #endif | |||
189 | */ | 190 | */ | ||
190 | parser.process(app); | 191 | parser.process(app); | ||
191 | 192 | | |||
192 | /** | 193 | /** | ||
193 | * handle standard options | 194 | * handle standard options | ||
194 | */ | 195 | */ | ||
195 | aboutData.processCommandLine(&parser); | 196 | aboutData.processCommandLine(&parser); | ||
196 | 197 | | |||
198 | KWriteApplication kapp; | ||||
199 | | ||||
197 | if (app.isSessionRestored()) { | 200 | if (app.isSessionRestored()) { | ||
198 | KWrite::restore(); | 201 | kapp.restore(); | ||
199 | } else { | 202 | } else { | ||
200 | bool nav = false; | 203 | bool nav = false; | ||
201 | int line = 0, column = 0; | 204 | int line = 0, column = 0; | ||
202 | 205 | | |||
203 | QTextCodec *codec = parser.isSet(QStringLiteral("encoding")) ? QTextCodec::codecForName(parser.value(QStringLiteral("encoding")).toLocal8Bit()) : nullptr; | 206 | QTextCodec *codec = parser.isSet(QStringLiteral("encoding")) ? QTextCodec::codecForName(parser.value(QStringLiteral("encoding")).toLocal8Bit()) : nullptr; | ||
204 | 207 | | |||
205 | if (parser.isSet(QStringLiteral("line"))) { | 208 | if (parser.isSet(QStringLiteral("line"))) { | ||
206 | line = parser.value(QStringLiteral("line")).toInt() - 1; | 209 | line = parser.value(QStringLiteral("line")).toInt() - 1; | ||
207 | nav = true; | 210 | nav = true; | ||
208 | } | 211 | } | ||
209 | 212 | | |||
210 | if (parser.isSet(QStringLiteral("column"))) { | 213 | if (parser.isSet(QStringLiteral("column"))) { | ||
211 | column = parser.value(QStringLiteral("column")).toInt() - 1; | 214 | column = parser.value(QStringLiteral("column")).toInt() - 1; | ||
212 | nav = true; | 215 | nav = true; | ||
213 | } | 216 | } | ||
214 | 217 | | |||
215 | if (parser.positionalArguments().count() == 0) { | 218 | if (parser.positionalArguments().count() == 0) { | ||
216 | KWrite *t = new KWrite; | 219 | KWrite *t = kapp.newWindow(); | ||
217 | 220 | | |||
218 | if (parser.isSet(QStringLiteral("stdin"))) { | 221 | if (parser.isSet(QStringLiteral("stdin"))) { | ||
219 | QTextStream input(stdin, QIODevice::ReadOnly); | 222 | QTextStream input(stdin, QIODevice::ReadOnly); | ||
220 | 223 | | |||
221 | // set chosen codec | 224 | // set chosen codec | ||
222 | if (codec) { | 225 | if (codec) { | ||
223 | input.setCodec(codec); | 226 | input.setCodec(codec); | ||
224 | } | 227 | } | ||
225 | 228 | | |||
226 | QString line; | 229 | QString line; | ||
227 | QString text; | 230 | QString text; | ||
228 | 231 | | |||
229 | do { | 232 | do { | ||
230 | line = input.readLine(); | 233 | line = input.readLine(); | ||
231 | text.append(line + QLatin1Char('\n')); | 234 | text.append(line + QLatin1Char('\n')); | ||
232 | } while (!line.isNull()); | 235 | } while (!line.isNull()); | ||
233 | 236 | | |||
234 | KTextEditor::Document *doc = t->view()->document(); | 237 | KTextEditor::Document *doc = t->activeView()->document(); | ||
235 | if (doc) { | 238 | if (doc) { | ||
236 | // remember codec in document, e.g. to show the right one | 239 | // remember codec in document, e.g. to show the right one | ||
237 | if (codec) { | 240 | if (codec) { | ||
238 | doc->setEncoding(QString::fromLatin1(codec->name())); | 241 | doc->setEncoding(QString::fromLatin1(codec->name())); | ||
239 | } | 242 | } | ||
240 | doc->setText(text); | 243 | doc->setText(text); | ||
241 | } | 244 | } | ||
242 | } | 245 | } | ||
243 | 246 | | |||
244 | if (nav && t->view()) { | 247 | if (nav && t->activeView()) { | ||
245 | t->view()->setCursorPosition(KTextEditor::Cursor(line, column)); | 248 | t->activeView()->setCursorPosition(KTextEditor::Cursor(line, column)); | ||
246 | } | 249 | } | ||
247 | } else { | 250 | } else { | ||
248 | int docs_opened = 0; | 251 | int docs_opened = 0; | ||
249 | Q_FOREACH(const QString positionalArgument, parser.positionalArguments()) { | 252 | Q_FOREACH(const QString positionalArgument, parser.positionalArguments()) { | ||
250 | UrlInfo info(positionalArgument); | 253 | UrlInfo info(positionalArgument); | ||
251 | if (nav) { | 254 | if (nav) { | ||
252 | info.cursor = KTextEditor::Cursor(line, column); | 255 | info.cursor = KTextEditor::Cursor(line, column); | ||
253 | } | 256 | } | ||
254 | 257 | | |||
255 | // this file is no local dir, open it, else warn | 258 | // this file is no local dir, open it, else warn | ||
256 | bool noDir = !info.url.isLocalFile() || !QFileInfo(info.url.toLocalFile()).isDir(); | 259 | bool noDir = !info.url.isLocalFile() || !QFileInfo(info.url.toLocalFile()).isDir(); | ||
257 | 260 | | |||
258 | if (noDir) { | 261 | if (noDir) { | ||
259 | ++docs_opened; | 262 | ++docs_opened; | ||
260 | KWrite *t = new KWrite(); | 263 | KWrite *t = kapp.newWindow(); | ||
261 | 264 | | |||
262 | if (codec) { | 265 | if (codec) { | ||
263 | t->view()->document()->setEncoding(QString::fromLatin1(codec->name())); | 266 | t->activeView()->document()->setEncoding(QString::fromLatin1(codec->name())); | ||
264 | } | 267 | } | ||
265 | 268 | | |||
266 | t->loadURL(info.url); | 269 | t->loadURL(info.url); | ||
267 | 270 | | |||
268 | if (info.cursor.isValid()) { | 271 | if (info.cursor.isValid()) { | ||
269 | t->view()->setCursorPosition(info.cursor); | 272 | t->activeView()->setCursorPosition(info.cursor); | ||
270 | } | 273 | } | ||
271 | else if (info.url.hasQuery()) { | 274 | else if (info.url.hasQuery()) { | ||
272 | QUrlQuery q(info.url); | 275 | QUrlQuery q(info.url); | ||
273 | QString lineStr = q.queryItemValue(QStringLiteral("line")); | 276 | QString lineStr = q.queryItemValue(QStringLiteral("line")); | ||
274 | QString columnStr = q.queryItemValue(QStringLiteral("column")); | 277 | QString columnStr = q.queryItemValue(QStringLiteral("column")); | ||
275 | 278 | | |||
276 | line = lineStr.toInt(); | 279 | line = lineStr.toInt(); | ||
277 | if (line > 0) | 280 | if (line > 0) | ||
278 | line--; | 281 | line--; | ||
279 | 282 | | |||
280 | column = columnStr.toInt(); | 283 | column = columnStr.toInt(); | ||
281 | if (column > 0) | 284 | if (column > 0) | ||
282 | column--; | 285 | column--; | ||
283 | 286 | | |||
284 | t->view()->setCursorPosition(KTextEditor::Cursor(line, column)); | 287 | t->activeView()->setCursorPosition(KTextEditor::Cursor(line, column)); | ||
285 | } | 288 | } | ||
286 | } else { | 289 | } else { | ||
287 | KMessageBox::sorry(nullptr, i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", info.url.toString())); | 290 | KMessageBox::sorry(nullptr, i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", info.url.toString())); | ||
288 | } | 291 | } | ||
289 | } | 292 | } | ||
290 | if (!docs_opened) { | 293 | if (!docs_opened) { | ||
291 | ::exit(1); // see http://bugs.kde.org/show_bug.cgi?id=124708 | 294 | ::exit(1); // see http://bugs.kde.org/show_bug.cgi?id=124708 | ||
292 | } | 295 | } | ||
293 | } | 296 | } | ||
294 | } | 297 | } | ||
295 | 298 | | |||
296 | // no window there, uh, ohh, for example borked session config !!! | 299 | // no window there, uh, ohh, for example borked session config !!! | ||
297 | // create at least one !! | 300 | // create at least one !! | ||
298 | if (KWrite::noWindows()) { | 301 | if (kapp.noWindows()) { | ||
299 | new KWrite(); | 302 | kapp.newWindow(); | ||
300 | } | 303 | } | ||
301 | 304 | | |||
302 | /** | 305 | /** | ||
303 | * finally register this kwrite instance for dbus, don't die if no dbus is around! | 306 | * finally register this kwrite instance for dbus, don't die if no dbus is around! | ||
304 | */ | 307 | */ | ||
305 | const KDBusService dbusService(KDBusService::Multiple | KDBusService::NoExitOnFailure); | 308 | const KDBusService dbusService(KDBusService::Multiple | KDBusService::NoExitOnFailure); | ||
306 | 309 | | |||
307 | /** | 310 | /** | ||
308 | * Run the event loop | 311 | * Run the event loop | ||
309 | */ | 312 | */ | ||
310 | return app.exec(); | 313 | return app.exec(); | ||
311 | } | 314 | } |