Commit 784a41b2 authored by Christoph Cullmann's avatar Christoph Cullmann

start to have again kind of working command line parsing

parent 9e5315e2
......@@ -41,6 +41,7 @@
#include <QFileInfo>
#include <QTextCodec>
#include <QCommandLineParser>
#include <stdlib.h>
#include <sys/types.h>
......@@ -49,9 +50,9 @@
KateApp *KateApp::s_self = 0;
KateApp::KateApp()
KateApp::KateApp(const QCommandLineParser &args)
: m_shouldExit(false)
, m_args (0)
, m_args (args)
{
s_self = this;
......@@ -175,20 +176,16 @@ void KateApp::restoreKate ()
bool KateApp::startupKate ()
{
#if FIXME
// FIXME KF5
// user specified session to open
if (m_args->isSet ("startanon"))
if (m_args.isSet ("startanon"))
{
sessionManager()->activateSession (sessionManager()->giveSession (""), false, false);
}
else if (m_args->isSet ("start"))
else if (m_args.isSet ("start"))
{
sessionManager()->activateSession (sessionManager()->giveSession (m_args->getOption("start")), false, false);
sessionManager()->activateSession (sessionManager()->giveSession (m_args.value("start")), false, false);
}
else if (!m_args->isSet( "stdin" ) && (m_args->count() == 0)) // only start session if no files specified
else if (!m_args.isSet( "stdin" ) && (m_args.positionalArguments().count() == 0)) // only start session if no files specified
{
// let the user choose session if possible
if (!sessionManager()->chooseSession ())
......@@ -205,8 +202,6 @@ bool KateApp::startupKate ()
{
sessionManager()->activateSession( KateSession::Ptr(new KateSession (sessionManager(), QString())), false, false );
}
#endif
// oh, no mainwindow, create one, should not happen, but make sure ;)
if (mainWindows() == 0)
......@@ -217,38 +212,36 @@ bool KateApp::startupKate ()
KStartupInfo::setNewStartupId( activeMainWindow(), startupId());
#endif
#if FIXME
// FIXME KF5
QTextCodec *codec = m_args->isSet("encoding") ? QTextCodec::codecForName(m_args->getOption("encoding").toUtf8()) : 0;
QTextCodec *codec = m_args.isSet("encoding") ? QTextCodec::codecForName(m_args.value("encoding").toUtf8()) : 0;
bool tempfileSet = KCmdLineArgs::isTempFileSet();
KTextEditor::Document *doc = 0;
KateDocManager::self()->setSuppressOpeningErrorDialogs(true);
for (int z = 0; z < m_args->count(); z++)
Q_FOREACH (const QString positionalArgument, m_args.positionalArguments())
{
// convert to an url
const QUrl url (positionalArgument);
// this file is no local dir, open it, else warn
bool noDir = !m_args->url(z).isLocalFile() || !QFileInfo (m_args->url(z).toLocalFile()).isDir();
bool noDir = !url.isLocalFile() || !QFileInfo (url.toLocalFile()).isDir();
if (noDir)
{
// open a normal file
if (codec)
doc = activeMainWindow()->viewManager()->openUrl( m_args->url(z), codec->name(), false, tempfileSet);
doc = activeMainWindow()->viewManager()->openUrl( url, codec->name(), false, tempfileSet);
else
doc = activeMainWindow()->viewManager()->openUrl( m_args->url(z), QString(), false, tempfileSet);
doc = activeMainWindow()->viewManager()->openUrl( url, QString(), false, tempfileSet);
}
else
KMessageBox::sorry( activeMainWindow(),
i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", m_args->url(z).url()) );
i18n("The file '%1' could not be opened: it is not a normal file, it is a folder.", url.toString()) );
}
KateDocManager::self()->setSuppressOpeningErrorDialogs(false);
// handle stdin input
if( m_args->isSet( "stdin" ) )
if( m_args.isSet( "stdin" ) )
{
QTextStream input(stdin, QIODevice::ReadOnly);
......@@ -278,22 +271,20 @@ bool KateApp::startupKate ()
int column = 0;
bool nav = false;
if (m_args->isSet ("line"))
if (m_args.isSet ("line"))
{
line = m_args->getOption ("line").toInt() - 1;
line = m_args.value ("line").toInt() - 1;
nav = true;
}
if (m_args->isSet ("column"))
if (m_args.isSet ("column"))
{
column = m_args->getOption ("column").toInt() - 1;
column = m_args.value ("column").toInt() - 1;
nav = true;
}
if (nav && activeMainWindow()->viewManager()->activeView ())
activeMainWindow()->viewManager()->activeView ()->setCursorPosition (KTextEditor::Cursor (line, column));
#endif
// show the nice tips
KTipDialog::showTip(activeMainWindow());
......
......@@ -35,6 +35,7 @@ class KatePluginManager;
class KateDocManager;
class KateAppCommands;
class KateAppAdaptor;
class QCommandLineParser;
namespace KTextEditor
{
......@@ -63,7 +64,7 @@ class KATEINTERFACES_EXPORT KateApp : public QObject
/**
* application constructor
*/
KateApp ();
KateApp (const QCommandLineParser &args);
/**
* application destructor
......@@ -245,7 +246,7 @@ class KATEINTERFACES_EXPORT KateApp : public QObject
/**
* kate's command line args
*/
KCmdLineArgs *m_args;
const QCommandLineParser &m_args;
/**
* plugin interface
......
......@@ -37,6 +37,7 @@
#include <QDBusInterface>
#include <QDBusReply>
#include <QVariant>
#include <QCommandLineParser>
#include <iostream>
......@@ -121,42 +122,71 @@ extern "C" KDE_EXPORT int kdemain( int argc, char **argv )
* take component name and org. name from KAboutData
*/
QApplication app (argc, argv);
app.setApplicationName(aboutData.componentName());
app.setOrganizationDomain(aboutData.organizationDomain());
app.setApplicationName (aboutData.componentName());
app.setOrganizationDomain (aboutData.organizationDomain());
app.setApplicationVersion (aboutData.version());
app.setQuitOnLastWindowClosed (false);
/**
* Create command line parser and feed it with known options
*/
QCommandLineParser parser;
parser.setApplicationDescription (aboutData.displayName());
parser.addHelpOption ();
parser.addVersionOption ();
// -s/--start session option
const QCommandLineOption startSession (QStringList () << "s" << "start", i18n("Start Kate with a given session."), "session");
parser.addOption (startSession);
// --startanon session option
const QCommandLineOption startAnonymousSession (QStringList () << "startanon", i18n("Start Kate with a new anonymous session, implies '-n'."));
parser.addOption (startAnonymousSession);
// -n/--new option
const QCommandLineOption startNewInstance (QStringList () << "n" << "new", i18n("Force start of a new kate instance (is ignored if start is used and another kate instance already has the given session opened), forced if no parameters and no URLs are given at all."));
parser.addOption (startNewInstance);
// -b/--block option
const QCommandLineOption startBlocking (QStringList () << "b" << "block", i18n("If using an already running kate instance, block until it exits, if URLs given to open."));
parser.addOption (startBlocking);
// -p/--pid option
const QCommandLineOption usePid (QStringList () << "p" << "pid", i18n("Only try to reuse kate instance with this pid (is ignored if start is used and another kate instance already has the given session opened)."), "pid");
parser.addOption (usePid);
// -e/--encoding option
const QCommandLineOption useEncoding (QStringList () << "e" << "encoding", i18n("Set encoding for the file to open."), "encoding");
parser.addOption (useEncoding);
// -l/--line option
const QCommandLineOption gotoLine (QStringList () << "l" << "line", i18n("Navigate to this line."), "line");
parser.addOption (gotoLine);
// -c/--column option
const QCommandLineOption gotoColumn (QStringList () << "c" << "column", i18n("Navigate to this column."), "column");
parser.addOption (gotoColumn);
// -i/--stdin option
const QCommandLineOption readStdIn (QStringList () << "i" << "stdin", i18n("Read the contents of stdin."));
parser.addOption (readStdIn);
// urls to open
parser.addPositionalArgument("urls", i18n("Documents to open."), "[urls...]");
// FIXME KF5 KCmdLineArgs::addCmdLineOptions (options);
// FIXME KF5 KCmdLineArgs::addTempFileOption();
/**
* do the command line parsing
*/
parser.process (app);
#ifdef TODO
// FIXME KF5
// command line args init and co
KCmdLineArgs::init (argc, argv, &aboutData);
KCmdLineOptions options;
options.add("s");
options.add("start <name>", i18n("Start Kate with a given session"));
options.add("startanon", i18n("Start Kate with a new anonymous session, implies '-n'"));
options.add("n");
options.add("new", i18n("Force start of a new kate instance (is ignored if start is used and another kate instance already has the given session opened), forced if no parameters and no URLs are given at all"));
options.add("b");
options.add("block", i18n("If using an already running kate instance, block until it exits, if URLs given to open"));
options.add("p");
options.add("pid <pid>", i18n("Only try to reuse kate instance with this pid (is ignored if start is used and another kate instance already has the given session opened)"));
options.add("e");
options.add("encoding <name>", i18n("Set encoding for the file to open"));
options.add("l");
options.add("line <line>", i18n("Navigate to this line"));
options.add("c");
options.add("column <column>", i18n("Navigate to this column"));
options.add("i");
options.add("stdin", i18n("Read the contents of stdin"));
options.add("u");
options.add("use", i18n("Reuse existing Kate instance; default, only for compatibility"));
options.add("+[URL]", i18n("Document to open"));
KCmdLineArgs::addCmdLineOptions (options);
KCmdLineArgs::addTempFileOption();
// get our command line args ;)
KCmdLineArgs* args = KCmdLineArgs::parsedArgs();
......@@ -357,18 +387,6 @@ extern "C" KDE_EXPORT int kdemain( int argc, char **argv )
QDBusMessage activateMsg = QDBusMessage::createMethodCall (serviceName,
QLatin1String("/MainApplication"), "org.kde.Kate.Application", "activate");
QDBusConnection::sessionBus().call (activateMsg);
// application object to have event loop
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// It's too bad, that we have to use KApplication here, since this forces us to
// register a service to dbus. If we don't use KApplication we cannot use KStartupInfo
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//
KApplication app(true);
// do no session management for this app, just client
app.disableSessionManagement ();
// connect dbus signal
if (needToBlock) {
......@@ -390,8 +408,9 @@ extern "C" KDE_EXPORT int kdemain( int argc, char **argv )
/**
* construct the real kate app object ;)
* behaves like a singleton, one unique instance
* we are passing our local command line parser to it
*/
KateApp kateApp;
KateApp kateApp (parser);
if (kateApp.shouldExit()) return 0;
/**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment