00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifdef HAVE_CONFIG_H
00020 #include "config.h"
00021 #endif
00022
00023 #if TIME_WITH_SYS_TIME
00024 # include <sys/time.h>
00025 # include <time.h>
00026 #else
00027 # if HAVE_SYS_TIME_H
00028 # include <sys/time.h>
00029 # else
00030 # include <time.h>
00031 # endif
00032 #endif
00033
00034 #include <kcmdlineargs.h>
00035 #include <kaboutdata.h>
00036 #include <klocale.h>
00037 #include <kglobal.h>
00038 #include <kconfig.h>
00039 #include <kstandarddirs.h>
00040 #include <kdebug.h>
00041
00042 #include <libkcal/calformat.h>
00043 #include <libkcal/calendarresources.h>
00044 #include <libkcal/resourcelocal.h>
00045
00046 #include <qdatetime.h>
00047 #include <qfile.h>
00048
00049 #include <stdlib.h>
00050 #include <iostream>
00051
00052 #include "konsolekalendar.h"
00053 #include "konsolekalendarepoch.h"
00054
00055 #include "konsolekalendarvariables.h"
00056
00057 using namespace KCal;
00058 using namespace std;
00059
00060 static const char progName[] = "konsolekalendar";
00061 static const char progDisplay[] = "KonsoleKalendar";
00062 static const char progVersion[] = "1.0.2";
00063 static const char description[] = I18N_NOOP("A command line interface to KDE calendars");
00064
00065 static KCmdLineOptions options[] =
00066 {
00067 { "help", I18N_NOOP("Print this help and exit"), 0 },
00068 { "verbose", I18N_NOOP("Print helpful runtime messages"), 0 },
00069 { "dry-run", I18N_NOOP("Print what would have been done, but do not execute"), 0 },
00070 { "file <calendar-file>", I18N_NOOP("Specify which calendar you want to use"), 0 },
00071
00072
00073 { ":", I18N_NOOP(" Major operation modes:"), 0 },
00074 { "view", I18N_NOOP(" Print calendar events in specified export format"), 0 },
00075 { "add", I18N_NOOP(" Insert an event into the calendar"), 0 },
00076 { "change", I18N_NOOP(" Modify an existing calendar event"), 0 },
00077 { "delete", I18N_NOOP(" Remove an existing calendar event"), 0 },
00078 { "create", I18N_NOOP(" Create new calendar file if one does not exist"), 0 },
00079 { "import <import-file>", I18N_NOOP(" Import this calendar to main calendar"), 0 },
00080
00081 { ":", I18N_NOOP(" Operation modifiers:"), 0 },
00082 { "next", I18N_NOOP(" View next activity in calendar"), 0 },
00083 { "all", I18N_NOOP(" View all calendar entries"), 0 },
00084 { "uid <uid>", I18N_NOOP(" Event Unique-string identifier"), 0 },
00085 { "show-next <show-next>", I18N_NOOP(" From this day show next # days' activities"), 0 },
00086 { "date <start-date>", I18N_NOOP(" Start from this day [YYYY-MM-DD]"), 0 },
00087 { "time <start-time>", I18N_NOOP(" Start from this time [HH:MM:SS]"), 0 },
00088 { "end-date <end-date>", I18N_NOOP(" End at this day [YYYY-MM-DD]"), 0 },
00089 { "end-time <end-time>", I18N_NOOP(" End at this time [HH:MM:SS]"), 0 },
00090 { "epoch-start <epoch-time>", I18N_NOOP(" Start from this time [secs since epoch]"), 0 },
00091 { "epoch-end <epoch-time>", I18N_NOOP(" End at this time [secs since epoch]"), 0 },
00092 { "summary <summary>", I18N_NOOP(" Add summary to event (for add/change modes)"), 0 },
00093 { "description <description>", I18N_NOOP("Add description to event (for add/change modes)"), 0 },
00094
00095 { ":", I18N_NOOP(" Export options:"), 0 },
00096 { "export-type <export-type>", I18N_NOOP("Export file type (Default: text)"), 0 },
00097 { "export-file <export-file>", I18N_NOOP("Export to file (Default: stdout)"), 0 },
00098 { "export-list", I18N_NOOP(" Print list of export types supported and exit"), 0 },
00099
00100 { "", I18N_NOOP("Examples:\n konsolekalendar --view\n konsolekalendar --add --date 2003-06-04 --time 10:00 --end-time 12:00 \\\n --summary \"Doctor Visit\" --description \"Get My Head Examined\"\n konsolekalendar --delete --uid KOrganizer-1740380426.803"), 0 },
00101
00102 KCmdLineLastOption
00103 };
00104
00105 int main(int argc, char *argv[])
00106 {
00107 KLocale::setMainCatalogue("konsolekalendar");
00108
00109 KAboutData aboutData(
00110 progName,
00111 I18N_NOOP( progDisplay ),
00112 progVersion,
00113 description,
00114 KAboutData::License_GPL,
00115 "(c) 2002-2004, Tuukka Pasanen and Allen Winter",
00116 0,
00117 "http://pim.kde.org",
00118 "bugs.kde.org"
00119 );
00120
00121 aboutData.addAuthor(
00122 "Tuukka Pasanen",
00123 I18N_NOOP("Primary Author"),
00124 "illuusio@mailcity.com",
00125 0
00126 );
00127 aboutData.addAuthor(
00128 "Allen Winter",
00129 I18N_NOOP("Author"),
00130 "awinterz@users.sourceforge.net",
00131 0
00132 );
00133
00134
00135
00136
00137
00138 KCmdLineArgs::init( argc, argv, &aboutData, true );
00139 KCmdLineArgs::addCmdLineOptions( options );
00140
00141 KInstance ins(progName);
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151 KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00152
00153 QString KalendarFile;
00154
00155
00156 QDate startdate = QDate::currentDate();
00157 QTime starttime(7,0);
00158
00159
00160 QDate enddate = QDate::currentDate();
00161 QTime endtime(17,0);
00162
00163
00164 bool view = true;
00165 bool add = false;
00166 bool change = false;
00167 bool del = false;
00168 bool create = false;
00169 bool calendarFile = false;
00170 bool importFile = false;
00171
00172 QString option;
00173
00174 KonsoleKalendarVariables variables;
00175 KonsoleKalendarEpoch epochs;
00176
00177
00178 variables.setExportType( NONE );
00179
00180 if ( args->isSet("verbose") ) {
00181 variables.setVerbose(true);
00182 }
00183
00184 if ( args->isSet("dry-run") ) {
00185 variables.setDryRun(true);
00186 }
00187
00188
00189
00190
00191
00192 if ( args->isSet("export-list") ) {
00193 cout << i18n("\nKonsoleKalendar supports these export formats:\n Text [Default]\n HTML\n CSV (Comma-Separated Values)").local8Bit() << endl;
00194 return(0);
00195 }
00196
00197
00198
00199
00200
00201 if ( args->isSet("export-type") ) {
00202 option = args->getOption("export-type");
00203
00204 if ( option.upper() == "HTML" ) {
00205 kdDebug() << "main | export-type | Export to HTML" << endl;
00206 variables.setExportType( HTML );
00207 } else if( option.upper() == "CSV" ) {
00208 kdDebug() << "main | export-type | Export to CSV" << endl;
00209 variables.setExportType( CSV );
00210 } else if( option.upper() == "TEXT" ) {
00211 kdDebug() << "main | exporttype | Export to TXT" << endl;
00212 variables.setExportType( TEXT_KONSOLEKALENDAR );
00213 } else {
00214 cout << i18n("Invalid Export Type Specified: ").local8Bit()
00215 << option.local8Bit() << endl;
00216 return(1);
00217 }
00218 }
00219
00220
00221
00222
00223
00224 if ( args->isSet("export-file") ) {
00225 option = args->getOption("export-file");
00226
00227 kdDebug() << "main | parse options | Export File: (" << option << ")" << endl;
00228
00229 variables.setExportFile(option);
00230 }
00231
00232
00233
00234
00235
00236 if ( args->isSet("view") ) {
00237 view=true;
00238
00239 kdDebug() << "main | parse options | Mode: (Print events)" << endl;
00240 }
00241
00242
00243
00244
00245
00246 if ( args->isSet("add") ) {
00247 view=false;
00248 add=true;
00249
00250 kdDebug() << "main | parse options | Mode: (Add event)" << endl;
00251 }
00252
00253
00254
00255
00256
00257 if ( args->isSet("change") ) {
00258 view=false;
00259 change=true;
00260
00261 kdDebug() << "main | parse options | Mode: (Change event)" << endl;
00262 }
00263
00264
00265
00266
00267
00268 if ( args->isSet("delete") ) {
00269 view=false;
00270 del=true;
00271
00272 kdDebug() << "main | parse options | Mode: (Delete event)" << endl;
00273 }
00274
00275
00276
00277
00278
00279 if ( args->isSet("create") ) {
00280 view=false;
00281 create=true;
00282
00283 kdDebug() << "main | parse options | Calendar File: (Create)" << endl;
00284
00285 }
00286
00287
00288
00289
00290
00291
00292 if ( args->isSet("summary") ) {
00293 option = args->getOption("summary");
00294
00295 kdDebug() << "main | parse options | Summary: (" << option << ")" << endl;
00296
00297 variables.setSummary(option);
00298 }
00299
00300
00301
00302
00303
00304 if ( args->isSet("description") ) {
00305 option = args->getOption("description");
00306
00307 kdDebug() << "main | parse options | Description: (" << option << ")" << endl;
00308
00309 variables.setDescription(option);
00310 }
00311
00312
00313
00314
00315
00316 if ( args->isSet("next") )
00317 {
00318 kdDebug() << "main | parse options | Show next event only" << endl;
00319
00320 variables.setNext(true);
00321 }
00322
00323
00324
00325
00326
00327
00328 if (args->isSet("uid") ) {
00329 option = args->getOption("uid");
00330
00331 kdDebug() << "main | parse options | Event UID: (" << option << ")" << endl;
00332
00333 variables.setUID( option );
00334 }
00335
00336
00337
00338
00339
00340 if ( args->isSet("date") ) {
00341 option = args->getOption("date");
00342
00343 kdDebug() << "main | parse options | Start date before conversion: (" << option << ")" << endl;
00344
00345 startdate = QDate::fromString( option, Qt::ISODate );
00346 if( ! startdate.isValid() ) {
00347 cout << i18n("Invalid Start Date Specified: ").local8Bit()
00348 << option.local8Bit() << endl;
00349 return(1);
00350 }
00351 kdDebug() << "main | parse options | Start date after conversion: (" << startdate.toString() << ")" << endl;
00352 }
00353
00354
00355
00356
00357
00358
00359 if ( args->isSet("time") ) {
00360 option = args->getOption("time");
00361
00362 kdDebug() << "main | parse options | Start time before conversion : (" << option << ")" << endl;
00363
00364 starttime = QTime::fromString( option, Qt::ISODate );
00365 if( ! starttime.isValid() ) {
00366 cout << i18n("Invalid Start Time Specified: ").local8Bit()
00367 << option.local8Bit() << endl;
00368 return(1);
00369 }
00370 kdDebug() << "main | parse options | Start time after conversion: (" << starttime.toString() << ")" << endl;
00371 }
00372
00373
00374
00375
00376
00377
00378 if ( args->isSet("end-date") ) {
00379 QString option = args->getOption("end-date");
00380
00381 kdDebug() << "main | parse options | End date before conversion: (" << option << ")" << endl;
00382
00383 enddate = QDate::fromString( option, Qt::ISODate );
00384 if( ! enddate.isValid() ) {
00385 cout << i18n("Invalid End Date Specified: ").local8Bit()
00386 << option.local8Bit() << endl;
00387 return(1);
00388 }
00389 kdDebug() << "main | parse options | End date after conversion: (" << enddate.toString() << ")" << endl;
00390 }
00391
00392
00393
00394
00395
00396
00397 if ( args->isSet("show-next") )
00398 {
00399
00400 bool ok;
00401
00402 option = args->getOption("show-next");
00403 kdDebug() << "main | parse options | Show " << option << " days ahead" << endl;
00404 variables.setDaysCount( option.toInt( &ok, 10 ) );
00405
00406 if( !ok ) {
00407 cout << i18n("Invalid Date Count Specified: ").local8Bit()
00408 << option.local8Bit() << endl;
00409 return(1);
00410 }
00411
00412 enddate = startdate;
00413 enddate = enddate.addDays( variables.getDaysCount() );
00414 kdDebug() << "main | parse options | End date after conversion: (" << enddate.toString() << ")" << endl;
00415
00416 }
00417
00418
00419
00420
00421
00422 if ( args->isSet("end-time") ) {
00423 option = args->getOption("end-time");
00424
00425 kdDebug() << "main | parse options | End time before conversion: (" << option << ")" << endl;
00426
00427 endtime = QTime::fromString( option, Qt::ISODate );
00428 if( ! endtime.isValid() ) {
00429 cout << i18n("Invalid End Time Specified: ").local8Bit()
00430 << option.local8Bit() << endl;
00431 return(1);
00432 }
00433
00434 kdDebug() << "main | parse options | End time after conversion: (" << endtime.toString() << ")" << endl;
00435 }
00436
00437
00438
00439
00440
00441 time_t epochstart=0;
00442 if ( args->isSet("epoch-start") ) {
00443 option = args->getOption("epoch-start");
00444
00445 kdDebug() << "main | parse options | Epoch start: (" << option << ")" << endl;
00446
00447 epochstart = (time_t) option.toULong(0,10);
00448 }
00449
00450
00451
00452
00453
00454 time_t epochend=0;
00455 if ( args->isSet("epoch-end") ) {
00456 option = args->getOption("epoch-end");
00457
00458 kdDebug() << "main | parse options | Epoch end: (" << option << ")" << endl;
00459
00460 epochend = (time_t) option.toULong(0,10);
00461 }
00462
00463 if( args->isSet("all") ) {
00464 variables.setAll( true );
00465 } else {
00466 variables.setAll( false );
00467 }
00468
00469 if ( args->isSet("import") ) {
00470 view = false;
00471 importFile = true;
00472 option = args->getOption("import");
00473 variables.setImportFile( option );
00474
00475 kdDebug() << "main | parse options | importing file from: (" << option << ")" << endl;
00476 }
00477
00478 KonsoleKalendar *konsolekalendar = new KonsoleKalendar( &variables );
00479
00480 if ( args->isSet("file") ) {
00481 calendarFile = true;
00482 option = args->getOption("file");
00483 variables.setCalendarFile( option );
00484
00485
00486
00487
00488
00489 bool exists = QFile::exists( variables.getCalendarFile() );
00490
00491 if ( create ) {
00492
00493 kdDebug() << "main | createcalendar | check if calendar file already exists" << endl;
00494
00495 if( exists ) {
00496 cout << i18n("Calendar ").local8Bit()
00497 << variables.getCalendarFile().local8Bit()
00498 << i18n(" already exists").local8Bit() << endl;
00499 return(1);
00500 }
00501 if( konsolekalendar->createCalendar() ) {
00502 cout << i18n("Calendar ").local8Bit()
00503 << variables.getCalendarFile().local8Bit()
00504 << i18n(" successfully created.").local8Bit() << endl;
00505 return(0);
00506 } else {
00507 cout << i18n("Unable to create calendar: ").local8Bit()
00508 << variables.getCalendarFile().local8Bit() << endl;
00509 return(1);
00510 }
00511 }
00512
00513 if ( !exists ){
00514 cout << i18n("Calendar file not found").local8Bit()
00515 << option.local8Bit() << endl;
00516 cout << i18n("Try --create to create new calendar file").local8Bit()
00517 << endl;
00518 return(1);
00519 }
00520 }
00521
00522 CalendarResources *calendarResource = NULL;
00523 CalendarLocal *localCalendar = NULL;
00524
00525
00526
00527
00528 variables.setTimeZoneId();
00529 if( args->isSet("file") ) {
00530 localCalendar = new CalendarLocal( variables.getTimeZoneId() );
00531 localCalendar->load( variables.getCalendarFile() );
00532 variables.setCalendar( localCalendar );
00533 } else {
00534 calendarResource = new CalendarResources( variables.getTimeZoneId() );
00535 variables.setCalendarResources( calendarResource );
00536 }
00537
00538
00539
00540
00541 QDateTime startdatetime, enddatetime;
00542
00543
00544 if( !args->isSet("end-date") && args->isSet("date") ) {
00545 enddate = startdate;
00546 kdDebug() << "main | datetimestamp | setting enddate to startdate" << endl;
00547 } else if( args->isSet("end-date") && !args->isSet("date") ) {
00548 startdate = enddate;
00549 kdDebug() << "main | datetimestamp | setting startdate to enddate" << endl;
00550 }
00551
00552
00553
00554
00555
00556 if( !args->isSet("end-time") && !args->isSet("epoch-end") ) {
00557 if( args->isSet("time") ) {
00558 endtime = starttime.addSecs(60*60);
00559 kdDebug() << "main | datetimestamp | setting endtime 1 hour after starttime" << endl;
00560 } else if( args->isSet("epoch-start") ) {
00561 startdatetime = epochs.epoch2QDateTime(epochstart);
00562 enddatetime = startdatetime.addSecs(60*60);
00563 kdDebug() << "main | datetimestamp | setting endtime 1 hour after epochstart" << endl;
00564 }
00565 }
00566
00567
00568
00569 if( !args->isSet("time") && !args->isSet("epoch-start") ) {
00570 if( args->isSet("end-time") ) {
00571 starttime = endtime.addSecs(-60*60);
00572 kdDebug() << "main | datetimestamp | setting starttime 1 hour before endtime" << endl;
00573 } else if( args->isSet("epoch-end") ) {
00574 enddatetime = epochs.epoch2QDateTime(epochend);
00575 startdatetime = enddatetime.addSecs(-60*60);
00576 kdDebug() << "main | datetimestamp | setting starttime 1 before after epochend" << endl;
00577 }
00578 }
00579
00580
00581 if( !args->isSet("time") && !args->isSet("epoch-start") &&
00582 !args->isSet("end-time") && !args->isSet("epoch-end") ) {
00583
00584 startdatetime = QDateTime::QDateTime(startdate, starttime);
00585 kdDebug() << "main | datetimestamp | setting startdatetime from default startdate (today) and starttime" << endl;
00586
00587 enddatetime = QDateTime::QDateTime(enddate, endtime);
00588 kdDebug() << "main | datetimestamp | setting enddatetime from default enddate (today) and endtime" << endl;
00589 }
00590
00591
00592 if( startdatetime.isNull() ) {
00593 startdatetime = QDateTime::QDateTime(startdate, starttime);
00594 kdDebug() << "main | datetimestamp | setting startdatetime from startdate and starttime" << endl;
00595 }
00596 if( enddatetime.isNull() ) {
00597 enddatetime = QDateTime::QDateTime(enddate, endtime);
00598 kdDebug() << "main | datetimestamp | setting enddatetime from enddate and endtime" << endl;
00599 }
00600
00601
00602 variables.setStartDateTime( startdatetime );
00603 variables.setEndDateTime( enddatetime );
00604
00605
00606 kdDebug() << "main | datetimestamp | StartDate=" << startdatetime.toString(Qt::TextDate) << endl;
00607 kdDebug() << "main | datetimestamp | EndDate=" << enddatetime.toString(Qt::TextDate) << endl;
00608
00609
00610
00611
00612
00613
00614 if( create + view + add + change + del > 1 ) {
00615 cout << i18n("Only 1 operation mode (view, add, change, delete,create) permitted at a time").local8Bit() << endl;
00616 return(1);
00617 }
00618
00619
00620 if( startdatetime > enddatetime ) {
00621 cout << i18n("Ending Date/Time occurs before the Starting Date/Time").local8Bit() << endl;
00622 return(1);
00623 }
00624
00625
00626
00627
00628
00629
00630 if( add ) {
00631
00632
00633 if( args->isSet("time") || args->isSet("end-time") ||
00634 args->isSet("epoch-start") || args->isSet("epoch-end") ) {
00635 variables.setFloating(false);
00636 kdDebug() << "main | floatingcheck | turn-off floating event" << endl;
00637 }
00638 }
00639
00640 args->clear();
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652 QString prodId = "-//K Desktop Environment//NONSGML %1 %2//EN";
00653 CalFormat::setApplication( progDisplay, prodId.arg( progDisplay).arg( progVersion ) );
00654
00655 if( importFile ) {
00656 if( konsolekalendar->importCalendar() ) {
00657 cout << i18n("Calendar ").local8Bit()
00658 << variables.getImportFile().local8Bit()
00659 << i18n(" successfully imported.").local8Bit() << endl;
00660 return(0);
00661 } else {
00662 cout << i18n("Unable to import calendar: ").local8Bit()
00663 << variables.getImportFile().local8Bit() << endl;
00664 return(1);
00665 }
00666 }
00667
00668 if( add ) {
00669 if( !konsolekalendar->isEvent( startdatetime, enddatetime, variables.getSummary() ) ) {
00670 kdDebug() << "main | modework | calling addEvent()" << endl;
00671 konsolekalendar->addEvent();
00672 } else {
00673 cout << i18n("Attempting to insert an event that already exists").local8Bit() << endl;
00674 return(1);
00675 }
00676 }
00677
00678 if( change ) {
00679 kdDebug() << "main | modework | calling changeEvent()" << endl;
00680 if( !variables.isUID() ) {
00681 cout << i18n("Must specify a UID with --uid to change event").local8Bit() << endl;
00682 return(1);
00683 }
00684 if( konsolekalendar->changeEvent() != true ) {
00685 cout << i18n("Attempting to change a non-existent event").local8Bit() << endl;
00686 return(1);
00687 }
00688 kdDebug() << "main | modework | succesful changeEvent()" << endl;
00689 }
00690
00691 if( del ) {
00692 kdDebug() << "main | modework | calling deleteEvent()" << endl;
00693 if( !variables.isUID() ) {
00694 cout << i18n("Must specify a UID with --uid to delete event").local8Bit() << endl;
00695 return(1);
00696 }
00697 if( konsolekalendar->deleteEvent() != true ) {
00698 cout << i18n("Attempting to delete a non-existent event").local8Bit() << endl;
00699 return(1);
00700 }
00701 kdDebug() << "main | modework | succesful deleteEvent()" << endl;
00702 }
00703
00704 if( view ) {
00705 kdDebug() << "main | modework | calling showInstance() to view events" << endl;
00706 if( !konsolekalendar->showInstance() ) {
00707 cout << i18n("Cannot open specified export file: ").local8Bit()
00708 << variables.getExportFile().local8Bit() << endl;
00709 return(1);
00710 }
00711 }
00712
00713 delete konsolekalendar;
00714
00715 if( calendarFile ){
00716 localCalendar->close();
00717 delete localCalendar;
00718 } else {
00719 calendarResource->close();
00720 delete calendarResource;
00721 }
00722
00723 kdDebug() << "main | exiting" << endl;
00724
00725 return 0;
00726 }