Documentation: Completed Backup section, update menu.
[kdepim.git] / ktimetracker / main.cpp
blob9c62f982e25133189a674e2ed860f8534c5b35b4
1 /*
2 * Copyright (C) 1997 by Stephan Kulow <coolo@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the
16 * Free Software Foundation, Inc.
17 * 51 Franklin Street, Fifth Floor
18 * Boston, MA 02110-1301 USA.
22 #include "desktoplist.h"
23 #include <iostream>
24 #include <signal.h>
25 #include <QFile>
26 #include <KAboutData>
27 #include <KCmdLineArgs>
28 #include <KDebug>
29 #include <KLocale>
30 #include <KStandardDirs>
31 #include <kontactinterface/pimuniqueapplication.h>
33 #include "kdepim-version.h"
34 #include "mainwindow.h"
35 #include "mainadaptor.h"
36 #include "timetrackerstorage.h"
37 #include "task.h"
38 #include <QDebug>
40 namespace
42 const char* description = I18N_NOOP("KDE Time tracker tool");
44 void cleanup( int )
46 kDebug(5970) << i18n("Just caught a software interrupt.");
47 kapp->exit();
51 QString icsfile( const KCmdLineArgs* args ) // deliver the name of the iCalendar file to be used
53 QString result;
54 if ( args->count() > 0 ) // file is given as parameter
56 result = args->arg( 0 );
57 KUrl* icsfileurl=new KUrl(args->arg( 0 ));
58 if (( icsfileurl->protocol() == "http" ) || ( icsfileurl->protocol() == "ftp" ) || ( icsfileurl->isLocalFile() ))
60 // leave as is
63 else
65 result = KCmdLineArgs::cwd() + '/' + result;
67 delete icsfileurl;
69 else // file is not given as parameter
71 result=QString(KStandardDirs::locate( "data", "ktimetracker/ktimetracker.ics" ));
72 if ( !QFile::exists( result ) )
74 QFile oldFile( KStandardDirs::locate( "data", "karm/karm.ics" ) );
75 result = KStandardDirs::locateLocal( "appdata", QString::fromLatin1( "ktimetracker.ics" ) );
76 if ( oldFile.exists() )
77 oldFile.copy( result );
80 return result;
83 int main( int argc, char *argv[] )
85 KAboutData aboutData( "ktimetracker", 0, ki18n("KTimeTracker"),
86 KDEPIM_VERSION, ki18n(description), KAboutData::License_GPL,
87 ki18n("Copyright © 1997-2012 KDE PIM authors"), KLocalizedString(),
88 QByteArray("http://userbase.kde.org/KTimeTracker") );
90 aboutData.addAuthor( ki18n("Thorsten Stärk"), ki18n( "Current Maintainer" ),
91 "kde@staerk.de" );
92 aboutData.addAuthor( ki18n("Sirtaj Singh Kang"), ki18n( "Original Author" ),
93 "taj@kde.org" );
94 aboutData.addAuthor( ki18n("Allen Winter"), KLocalizedString(), "winter@kde.org" );
95 aboutData.addAuthor( ki18n("David Faure"), KLocalizedString(), "faure@kde.org" );
96 aboutData.addAuthor( ki18n("Mathias Soeken"), KLocalizedString(), "msoeken@tzi.de" );
97 aboutData.addAuthor( ki18n("Jesper Pedersen"), KLocalizedString(), "blackie@kde.org" );
98 aboutData.addAuthor( ki18n("Kalle Dalheimer"), KLocalizedString(), "kalle@kde.org" );
99 aboutData.addAuthor( ki18n("Mark Bucciarelli"), KLocalizedString(), "mark@hubcapconsulting.com" );
100 KCmdLineArgs::init( argc, argv, &aboutData );
102 KCmdLineOptions options;
103 options.add("+file", ki18n( "The iCalendar file to open" ));
104 options.add("listtasknames", ki18n( "List all tasks as text output" ));
105 options.add("addtask <taskname>", ki18n( "Add task <taskname>" ));
106 options.add("deletetask <taskid>", ki18n( "Delete task <taskid>" ));
107 options.add("taskidsfromname <taskname>", ki18n( "Print the task ids for all tasks named <taskname>" ));
108 options.add("starttask <taskid>", ki18n( "Start timer for task <taskid>" ));
109 options.add("stoptask <taskid>", ki18n( "Stop timer for task <taskid>" ));
110 options.add("totalminutesfortaskid <taskid>", ki18n( "Deliver total minutes for task id" ));
111 options.add("version", ki18n( "Outputs the version" ));
112 KCmdLineArgs::addCmdLineOptions( options );
113 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
114 int err=0; // error code
115 bool konsolemode=false; // open a gui and wait for user input?
116 if ( args->isSet("listtasknames") ) konsolemode=true;
117 if ( !args->getOption("addtask").isEmpty() ) konsolemode=true;
118 if ( !args->getOption("deletetask").isEmpty() ) konsolemode=true;
119 if ( !args->getOption("taskidsfromname").isEmpty() ) konsolemode=true;
120 if ( !args->getOption("totalminutesfortaskid").isEmpty() ) konsolemode=true;
121 if ( !args->getOption("starttask").isEmpty() ) konsolemode=true;
122 if ( !args->getOption("stoptask").isEmpty() ) konsolemode=true;
124 if ( !konsolemode )
125 { // no konsole mode
126 KApplication myApp;
127 MainWindow *mainWindow;
128 mainWindow = new MainWindow( icsfile( args ) );
129 if (kapp->isSessionRestored() && KMainWindow::canBeRestored( 1 ))
130 mainWindow->restore( 1, false );
131 else
132 mainWindow->show();
134 signal( SIGQUIT, cleanup );
135 signal( SIGINT, cleanup );
136 args->clear();
137 int ret = myApp.exec();
139 delete mainWindow;
140 return ret;
142 else // we are running in konsole mode
144 kDebug(5970) << "We are running in konsole mode";
145 KCmdLineArgs::addCmdLineOptions( options );
146 KApplication myApp(false);
147 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
148 // listtasknames
149 if ( args->isSet("listtasknames") )
151 timetrackerstorage* sto=new timetrackerstorage();
152 sto->load( 0, icsfile( args ) );
153 QStringList tasknameslist=sto->taskNames();
154 for ( int i=0; i<tasknameslist.count(); ++i )
156 char* line = tasknameslist[i].toLatin1().data();
157 std::cout << line << std::endl;
159 delete sto; // make valgrind happy
161 // addtask
162 if ( !args->getOption("addtask").isEmpty() )
164 timetrackerstorage* sto=new timetrackerstorage();
165 sto->load( 0, icsfile( args ) );
166 const QString& s=args->getOption("addtask");
167 QVector<int> vec;
168 DesktopList dl=vec;
169 Task* task=new Task( s, QString(), (long int) 0,(long int) 0, dl, 0, true );
170 sto->addTask( task );
171 sto->save( 0 );
172 delete sto;
174 // deletetask
175 if ( !args->getOption("deletetask").isEmpty() )
177 timetrackerstorage* sto=new timetrackerstorage();
178 sto->load( 0, icsfile( args ) );
179 const QString& taskid=args->getOption("deletetask");
180 sto->removeTask( taskid );
181 delete sto;
183 // taskidsfromname
184 if ( !args->getOption("taskidsfromname").isEmpty() )
186 timetrackerstorage* sto=new timetrackerstorage();
187 sto->load( 0, icsfile( args ) );
188 const QString& taskname=args->getOption("taskidsfromname");
189 QStringList taskids=sto->taskidsfromname( taskname );
190 for ( int i=0; i<taskids.count(); ++i )
192 char* line = taskids[i].toLatin1().data();
193 std::cout << line << std::endl;
195 delete sto;
197 // totalminutesfortaskid
198 if ( !args->getOption("totalminutesfortaskid").isEmpty() )
200 timetrackerstorage* sto=new timetrackerstorage();
201 sto->load( 0, icsfile( args ) );
202 Task* task=sto->task( args->getOption("totalminutesfortaskid"), 0 );
203 if (task!=0)
205 kDebug(5970) << "taskname=" << task->name();
206 std::cout << task->totalTime();
208 delete sto;
210 // starttask
211 if ( !args->getOption("starttask").isEmpty() )
213 timetrackerstorage* sto=new timetrackerstorage();
214 sto->load( 0, icsfile( args ) );
215 sto->startTimer(args->getOption("starttask"));
216 delete sto;
218 // stoptask
219 if ( !args->getOption("stoptask").isEmpty() )
221 timetrackerstorage* sto=new timetrackerstorage();
222 sto->load( 0, icsfile( args ) );
223 sto->stopTimer(sto->task( args->getOption("stoptask"), 0 ));
224 delete sto;
226 args->clear();
228 return err;