New Appointment -> New Task
[kdepim.git] / ktimetracker / main.cpp
blob415ad9e4536abf58e7139678d3ad81a66a8b28b8
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 "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 KTIMETRACKER_VERSION, ki18n(description), KAboutData::License_GPL,
87 ki18n("Copyright © 1997-2010 KDE PIM authors") );
89 aboutData.addAuthor( ki18n("Thorsten Stärk"), ki18n( "Current Maintainer" ),
90 "kde@staerk.de" );
91 aboutData.addAuthor( ki18n("Sirtaj Singh Kang"), ki18n( "Original Author" ),
92 "taj@kde.org" );
93 aboutData.addAuthor( ki18n("Allen Winter"), KLocalizedString(), "winter@kde.org" );
94 aboutData.addAuthor( ki18n("David Faure"), KLocalizedString(), "faure@kde.org" );
95 aboutData.addAuthor( ki18n("Mathias Soeken"), KLocalizedString(), "msoeken@tzi.de" );
96 aboutData.addAuthor( ki18n("Jesper Pedersen"), KLocalizedString(), "blackie@kde.org" );
97 aboutData.addAuthor( ki18n("Kalle Dalheimer"), KLocalizedString(), "kalle@kde.org" );
98 aboutData.addAuthor( ki18n("Mark Bucciarelli"), KLocalizedString(), "mark@hubcapconsulting.com" );
99 KCmdLineArgs::init( argc, argv, &aboutData );
101 KCmdLineOptions options;
102 options.add("+file", ki18n( "The iCalendar file to open" ));
103 options.add("listtasknames", ki18n( "List all tasks as text output" ));
104 options.add("addtask <taskname>", ki18n( "Add task <taskname>" ));
105 options.add("deletetask <taskid>", ki18n( "Delete task <taskid>" ));
106 options.add("taskidsfromname <taskname>", ki18n( "Print the task ids for all tasks named <taskname>" ));
107 options.add("starttask <taskid>", ki18n( "Start timer for task <taskid>" ));
108 options.add("stoptask <taskid>", ki18n( "Stop timer for task <taskid>" ));
109 options.add("totalminutesfortaskid <taskid>", ki18n( "Deliver total minutes for task id" ));
110 options.add("version", ki18n( "Outputs the version" ));
111 KCmdLineArgs::addCmdLineOptions( options );
112 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
113 int err=0; // error code
114 bool konsolemode=false; // open a gui and wait for user input?
115 if ( args->isSet("listtasknames") ) konsolemode=true;
116 if ( !args->getOption("addtask").isEmpty() ) konsolemode=true;
117 if ( !args->getOption("deletetask").isEmpty() ) konsolemode=true;
118 if ( !args->getOption("taskidsfromname").isEmpty() ) konsolemode=true;
119 if ( !args->getOption("totalminutesfortaskid").isEmpty() ) konsolemode=true;
120 if ( !args->getOption("starttask").isEmpty() ) konsolemode=true;
121 if ( !args->getOption("stoptask").isEmpty() ) konsolemode=true;
123 if ( !konsolemode )
124 { // no konsole mode
125 KApplication myApp;
126 MainWindow *mainWindow;
127 mainWindow = new MainWindow( icsfile( args ) );
128 if (kapp->isSessionRestored() && KMainWindow::canBeRestored( 1 ))
129 mainWindow->restore( 1, false );
130 else
131 mainWindow->show();
133 signal( SIGQUIT, cleanup );
134 signal( SIGINT, cleanup );
135 args->clear();
136 int ret = myApp.exec();
138 delete mainWindow;
139 return ret;
141 else // we are running in konsole mode
143 kDebug(5970) << "We are running in konsole mode";
144 KCmdLineArgs::addCmdLineOptions( options );
145 KApplication myApp(false);
146 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
147 // listtasknames
148 if ( args->isSet("listtasknames") )
150 timetrackerstorage* sto=new timetrackerstorage();
151 sto->load( 0, icsfile( args ) );
152 QStringList tasknameslist=sto->taskNames();
153 for ( int i=0; i<tasknameslist.count(); ++i )
155 char* line = tasknameslist[i].toLatin1().data();
156 std::cout << line << std::endl;
158 delete sto; // make valgrind happy
160 // addtask
161 if ( !args->getOption("addtask").isEmpty() )
163 timetrackerstorage* sto=new timetrackerstorage();
164 sto->load( 0, icsfile( args ) );
165 const QString& s=args->getOption("addtask");
166 QVector<int> vec;
167 DesktopList dl=vec;
168 Task* task=new Task( s,(long int) 0,(long int) 0, dl, 0, true );
169 sto->addTask( task );
170 sto->save( 0 );
171 delete sto;
173 // deletetask
174 if ( !args->getOption("deletetask").isEmpty() )
176 timetrackerstorage* sto=new timetrackerstorage();
177 sto->load( 0, icsfile( args ) );
178 const QString& taskid=args->getOption("deletetask");
179 sto->removeTask( taskid );
180 delete sto;
182 // taskidsfromname
183 if ( !args->getOption("taskidsfromname").isEmpty() )
185 timetrackerstorage* sto=new timetrackerstorage();
186 sto->load( 0, icsfile( args ) );
187 const QString& taskname=args->getOption("taskidsfromname");
188 QStringList taskids=sto->taskidsfromname( taskname );
189 for ( int i=0; i<taskids.count(); ++i )
191 char* line = taskids[i].toLatin1().data();
192 std::cout << line << std::endl;
194 delete sto;
196 // totalminutesfortaskid
197 if ( !args->getOption("totalminutesfortaskid").isEmpty() )
199 timetrackerstorage* sto=new timetrackerstorage();
200 sto->load( 0, icsfile( args ) );
201 Task* task=sto->task( args->getOption("totalminutesfortaskid"), 0 );
202 if (task!=0)
204 kDebug(5970) << "taskname=" << task->name();
205 std::cout << task->totalTime();
207 delete sto;
209 // starttask
210 if ( !args->getOption("starttask").isEmpty() )
212 timetrackerstorage* sto=new timetrackerstorage();
213 sto->load( 0, icsfile( args ) );
214 sto->startTimer(args->getOption("starttask"));
215 delete sto;
217 // stoptask
218 if ( !args->getOption("stoptask").isEmpty() )
220 timetrackerstorage* sto=new timetrackerstorage();
221 sto->load( 0, icsfile( args ) );
222 sto->stopTimer(sto->task( args->getOption("stoptask"), 0 ));
223 delete sto;
225 args->clear();
227 return err;