konsolekalendar Library API Documentation

main.cpp

00001 /*******************************************************************************
00002  * main.cpp                                                                    *
00003  *                                                                             *
00004  * KonsoleKalendar is a command line interface to KDE calendars                *
00005  * Copyright (C) 2002-2004  Tuukka Pasanen <illuusio@mailcity.com>             *
00006  * Copyright (C) 2003-2004  Allen Winter <awinterz@users.sourceforge.net>      *
00007  *                                                                             *
00008  * This program is free software; you can redistribute it and/or modify        *
00009  * it under the terms of the GNU General Public License as published by        *
00010  * the Free Software Foundation; either version 2 of the License, or           *
00011  * (at your option) any later version.                                         *
00012  *                                                                             *
00013  * This program is distributed in the hope that it will be useful,             *
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                *
00016  * GNU General Public License for more details.                                *
00017  *                                                                             *
00018  * You should have received a copy of the GNU General Public License           *
00019  * along with this program; if not, write to the Free Software                 *
00020  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. *
00021  *                                                                             *
00022  * As a special exception, permission is given to link this program            *
00023  * with any edition of Qt, and distribute the resulting executable,            *
00024  * without including the source code for Qt in the source distribution.        *
00025  *                                                                             *
00026  ******************************************************************************/
00027 
00028 #ifdef HAVE_CONFIG_H
00029 #include "config.h"
00030 #endif
00031 
00032 #if TIME_WITH_SYS_TIME
00033 # include <sys/time.h>
00034 # include <time.h>
00035 #else
00036 # if HAVE_SYS_TIME_H
00037 #  include <sys/time.h>
00038 # else
00039 #  include <time.h>
00040 # endif
00041 #endif
00042 
00043 #include <kcmdlineargs.h>
00044 #include <kaboutdata.h>
00045 #include <klocale.h>
00046 #include <kglobal.h>
00047 #include <kconfig.h>
00048 #include <kstandarddirs.h>
00049 #include <kdebug.h>
00050 
00051 #include <libkcal/calformat.h>
00052 #include <libkcal/calendarresources.h>
00053 #include <libkcal/resourcelocal.h>
00054 
00055 #include <qdatetime.h>
00056 #include <qfile.h>
00057 
00058 #include <stdlib.h>
00059 #include <iostream>
00060 
00061 #include "konsolekalendar.h"
00062 #include "konsolekalendarepoch.h"
00063 
00064 #include "konsolekalendarvariables.h"
00065 
00066 using namespace KCal;
00067 using namespace std;
00068 
00069 static const char progName[] = "konsolekalendar";
00070 static const char progDisplay[] = "KonsoleKalendar";
00071 static const char progVersion[] = "1.2.0";
00072 static const char progDesc[] = "A command line interface to KDE calendars";
00073 static const char progURL[] = "pim.kde.org/components/konsolekalendar.php";
00074 
00075 
00076 static KCmdLineOptions options[] =
00077 {
00078   { "help",
00079     I18N_NOOP( "Print this help and exit" ), 0 },
00080   { "verbose",
00081     I18N_NOOP( "Print helpful runtime messages" ), 0 },
00082   { "dry-run",
00083     I18N_NOOP( "Print what would have been done, but do not execute" ), 0 },
00084   { "file <calendar-file>",
00085     I18N_NOOP( "Specify which calendar you want to use" ), 0 },
00086 
00087   { ":",
00088     I18N_NOOP( "Major operation modes:" ), 0 },
00089   { "view",
00090     I18N_NOOP( "  Print calendar events in specified export format" ), 0 },
00091     { "add",
00092     I18N_NOOP( "  Insert an event into the calendar" ), 0 },
00093   { "change",
00094     I18N_NOOP( "  Modify an existing calendar event" ), 0 },
00095   { "delete",
00096     I18N_NOOP( "  Remove an existing calendar event" ), 0 },
00097   { "create",
00098     I18N_NOOP( "  Create new calendar file if one does not exist" ), 0 },
00099   { "import <import-file>",
00100     I18N_NOOP( "  Import this calendar to main calendar" ), 0 },
00101 
00102   { ":",
00103     I18N_NOOP( "Operation modifiers:" ), 0 },
00104   { "all",
00105     I18N_NOOP( "  View all calendar entries" ), 0 },
00106   { "next",
00107     I18N_NOOP( "  View next activity in calendar" ), 0 },
00108   { "show-next <days>",
00109     I18N_NOOP( "  From start date show next # days' activities" ), 0 },
00110   { "uid <uid>",
00111     I18N_NOOP( "  Event Unique-string identifier" ), 0 },
00112   { "date <start-date>",
00113     I18N_NOOP( "  Start from this day [YYYY-MM-DD]" ), 0 },
00114   { "time <start-time>",
00115     I18N_NOOP( "  Start from this time [HH:MM:SS]" ), 0 },
00116   { "end-date <end-date>",
00117     I18N_NOOP( "  End at this day [YYYY-MM-DD]" ), 0 },
00118   { "end-time <end-time>",
00119     I18N_NOOP( "  End at this time [HH:MM:SS]" ), 0 },
00120   { "epoch-start <epoch-time>",
00121     I18N_NOOP( " Start from this time [secs since epoch]" ), 0 },
00122   { "epoch-end <epoch-time>",
00123     I18N_NOOP( "  End at this time [secs since epoch]" ), 0 },
00124   { "summary <summary>",
00125     I18N_NOOP( "  Add summary to event (for add/change modes)" ), 0 },
00126   { "description <description>",
00127     I18N_NOOP( "Add description to event (for add/change modes)" ), 0 },
00128   { "location <location>",
00129     I18N_NOOP( "  Add location to event (for add/change modes)" ), 0 },
00130 
00131   { ":", I18N_NOOP( "Export options:" ), 0 },
00132   { "export-type <export-type>",
00133     I18N_NOOP( "Export file type (Default: text)" ), 0 },
00134   { "export-file <export-file>",
00135     I18N_NOOP( "Export to file (Default: stdout)" ), 0 },
00136   { "export-list",
00137     I18N_NOOP( "  Print list of export types supported and exit" ), 0 },
00138 
00139   { "",
00140     I18N_NOOP( "Examples:\n"
00141                "  konsolekalendar --view\n"
00142                "  konsolekalendar --add --date 2003-06-04 "
00143                "--time 10:00 --end-time 12:00 \\\n"
00144                "                  --summary \"Doctor Visit\" "
00145                "--description \"Get My Head Examined\"\n"
00146                "  konsolekalendar --delete --uid KOrganizer-1740326.803" ), 0 },
00147 
00148   { "",
00149     I18N_NOOP( "For more information visit the program home page at:\n"
00150                "  http://pim.kde.org/components/konsolekalendar.php" ), 0 },
00151 
00152   KCmdLineLastOption
00153 };
00154 
00155 int main( int argc, char *argv[] )
00156 {
00157   KAboutData aboutData(
00158     progName,                        // internal program name
00159     I18N_NOOP( progDisplay ),        // displayable program name.
00160     progVersion,                     // version string
00161     I18N_NOOP( progDesc ),           // short porgram description
00162     KAboutData::License_GPL,         // license type
00163     "(c) 2002-2004, Tuukka Pasanen and Allen Winter", // copyright statement
00164     0,                               // any free form text
00165     progURL,                         // program home page address
00166     "bugs.kde.org"                   // bug report email address
00167     );
00168 
00169   aboutData.addAuthor(
00170     "Tuukka Pasanen",                // developer's name
00171     I18N_NOOP( "Primary Author" ),   // task or role
00172     "illuusio@mailcity.com",         // email address
00173     0                                // home page or relevant link
00174     );
00175   aboutData.addAuthor(
00176     "Allen Winter",                  // developer's name
00177     I18N_NOOP( "Author" ),           // task or role
00178     "awinterz@users.sourceforge.net",// email address
00179     0                                // home page or relevant link
00180     );
00181 
00182 
00183 
00184   // KCmdLineArgs::init() final 'true' argument indicates no commandline options
00185   // for QApplication/KApplication (no KDE or Qt options)
00186   KCmdLineArgs::init( argc, argv, &aboutData, true );
00187   KCmdLineArgs::addCmdLineOptions( options ); // Add our own options.
00188 
00189   KInstance ins( progName );
00190 
00191 // Replace the KApplication call below with the three lines above
00192 // will make this a pure non-GUI application
00193 //   -- thanks for the info Stephan Kulow.
00194 
00195 //  KApplication app(
00196 //      false, //do not allowstyles - disable the loading on plugin based styles
00197 //      false  //GUI is not enabled - disable all GUI stuff
00198 //      );
00199 
00200   KCmdLineArgs *args = KCmdLineArgs::parsedArgs();
00201 
00202   // Default values for start date/time (today at 07:00)
00203   QDate startdate = QDate::currentDate();
00204   QTime starttime( 7 ,0 );
00205 
00206   // Default values for end date/time (today at 17:00)
00207   QDate enddate = QDate::currentDate();
00208   QTime endtime( 17, 0 );
00209 
00210   // Default values for switches
00211   bool view = true;
00212   bool add = false;
00213   bool change = false;
00214   bool del = false;
00215   bool create = false;
00216   bool calendarFile = false;
00217   bool importFile = false;
00218 
00219   QString option;
00220 
00221   KonsoleKalendarVariables variables;
00222   KonsoleKalendarEpoch epochs;
00223 
00224   variables.setExportType( NONE );
00225   variables.setFloating( false ); // by default, new events do NOT float
00226 
00227   if ( args->isSet( "verbose" ) ) {
00228     variables.setVerbose( true );
00229   }
00230 
00231   if ( args->isSet( "dry-run" ) ) {
00232     variables.setDryRun( true );
00233   }
00234 
00235   /*
00236    *  Switch on export list
00237    *
00238    */
00239   if ( args->isSet( "export-list" ) ) {
00240     cout << i18n(
00241       "\nKonsoleKalendar supports these export formats:\n"
00242       "  Text [Default]\n"
00243       "  Short (like Text, but more compact)\n"
00244       "  HTML\n"
00245       "  CSV (Comma-Separated Values)\n"
00246       ).local8Bit()
00247          << endl;
00248     return 0;
00249   }
00250 
00251   /*
00252    *  Switch on exporting
00253    *
00254    */
00255   if ( args->isSet( "export-type" ) ) {
00256     option = args->getOption( "export-type" );
00257 
00258     if ( option.upper() == "HTML" ) {
00259       kdDebug() << "main | export-type | Export to HTML" << endl;
00260       variables.setExportType( HTML );
00261     } else if ( option.upper() == "CSV" ) {
00262       kdDebug() << "main | export-type | Export to CSV" << endl;
00263       variables.setExportType( CSV );
00264     } else if ( option.upper() == "TEXT" ) {
00265       kdDebug() << "main | export-type | Export to TEXT (default)" << endl;
00266       variables.setExportType( TEXT_KONSOLEKALENDAR );
00267     } else if ( option.upper() == "SHORT" ) {
00268       kdDebug() << "main | export-type | Export to TEXT-SHORT" << endl;
00269       variables.setExportType( TEXT_SHORT );
00270     } else {
00271       cout << i18n( "Invalid Export Type Specified: %1" ).
00272         arg( option ).local8Bit()
00273            << endl;
00274       return 1;
00275     }
00276   }
00277 
00278   /*
00279    *  Switch on export file name
00280    *
00281    */
00282   if ( args->isSet( "export-file" ) ) {
00283     option = args->getOption( "export-file" );
00284 
00285     kdDebug() << "main | parse options | "
00286               << "Export File: "
00287               << "(" << option << ")"
00288               << endl;
00289 
00290     variables.setExportFile( option );
00291   }
00292 
00293   /*
00294    *  Switch on View (Print Entries).  This is the default mode of operation.
00295    *
00296    */
00297   if ( args->isSet( "view" ) ) {
00298     view=true;
00299 
00300     kdDebug() << "main | parse options | "
00301               << "Mode: (Print events)"
00302               << endl;
00303   }
00304 
00305   /*
00306    *  Switch on Add (Insert Entry)
00307    *
00308    */
00309   if ( args->isSet( "add" ) ) {
00310     view=false;
00311     add=true;
00312 
00313     kdDebug() << "main | parse options | "
00314               << "Mode: (Add event)"
00315               << endl;
00316   }
00317 
00318   /*
00319    *  Switch on Change (Modify Entry)
00320    *
00321    */
00322   if ( args->isSet( "change" ) ) {
00323     view=false;
00324     change=true;
00325 
00326     kdDebug() << "main | parse options | "
00327               << "Mode: (Change event)"
00328               << endl;
00329   }
00330 
00331   /*
00332    *  Switch on Delete (Remove Entry)
00333    *
00334    */
00335   if ( args->isSet( "delete" ) ) {
00336     view=false;
00337     del=true;
00338 
00339     kdDebug() << "main | parse options | "
00340               << "Mode: (Delete event)"
00341               << endl;
00342   }
00343 
00344   /*
00345    *  Switch on Create
00346    *
00347    */
00348   if ( args->isSet( "create" ) ) {
00349     view=false;
00350     create=true;
00351 
00352     kdDebug() << "main | parse options | "
00353               << "Calendar File: (Create)"
00354               << endl;
00355   }
00356 
00357 
00358   /*
00359    *  If there is summary attached.
00360    *
00361    */
00362   if ( args->isSet( "summary" ) ) {
00363     option = args->getOption( "summary" );
00364 
00365     kdDebug() << "main | parse options | "
00366               << "Summary: "
00367               << "(" << option << ")"
00368               << endl;
00369 
00370     variables.setSummary( option );
00371   }
00372 
00373   /*
00374    *  If there is description attached.
00375    *
00376    */
00377   if ( args->isSet( "description" ) ) {
00378     option = args->getOption( "description" );
00379 
00380     kdDebug() << "main | parse options | "
00381               << "Description: "
00382               << "(" << option << ")"
00383               << endl;
00384 
00385     variables.setDescription( option );
00386   }
00387 
00388   /*
00389    *  If there is location information
00390    *
00391    */
00392   if ( args->isSet( "location" ) ) {
00393     option = args->getOption( "location" );
00394 
00395     kdDebug() << "main | parse options | "
00396               << "Location: "
00397               << "(" << option << ")"
00398               << endl;
00399 
00400     variables.setLocation( option );
00401   }
00402 
00403   /*
00404    *  Show next happening and exit
00405    *
00406    */
00407   if ( args->isSet( "next" ) )
00408   {
00409     kdDebug() << "main | parse options | "
00410               << "Show next event only"
00411               << endl;
00412 
00413     variables.setNext( true );
00414   }
00415 
00416 
00417   /*
00418    *  Set event unique string identifier
00419    *
00420    */
00421   if (args->isSet( "uid" ) ) {
00422     option = args->getOption( "uid" );
00423 
00424     kdDebug() << "main | parse options | "
00425               << "Event UID: "
00426               << "(" << option << ")"
00427               << endl;
00428 
00429     variables.setUID( option );
00430   }
00431 
00432   /*
00433    *  Set starting date for calendar
00434    *
00435    */
00436   if ( args->isSet( "date" ) ) {
00437     option = args->getOption( "date" );
00438 
00439     kdDebug() << "main | parse options | "
00440               << "Start date before conversion: "
00441               << "(" << option << ")"
00442               << endl;
00443 
00444     startdate = QDate::fromString( option,  Qt::ISODate );
00445     if ( ! startdate.isValid() ) {
00446       cout << i18n( "Invalid Start Date Specified: %1" ).
00447         arg( option ).local8Bit()
00448            << endl;
00449       return 1;
00450     }
00451     kdDebug() << "main | parse options | "
00452               << "Start date after conversion: "
00453               << "(" << startdate.toString() << ")"
00454               << endl;
00455   }
00456 
00457   /*
00458    *  Set starting time
00459    *
00460    */
00461   if ( args->isSet( "time" ) ) {
00462     option = args->getOption( "time" );
00463 
00464     kdDebug() << "main | parse options | "
00465               << "Start time before conversion : "
00466               << "(" << option << ")"
00467               << endl;
00468 
00469     if ( option.upper() != "FLOAT" ) {
00470       starttime = QTime::fromString( option,  Qt::ISODate );
00471       if ( ! starttime.isValid() ) {
00472         cout << i18n( "Invalid Start Time Specified: %1" ).
00473           arg( option ).local8Bit()
00474              << endl;
00475         return 1;
00476       }
00477       kdDebug() << "main | parse options | "
00478                 << "Start time after conversion: "
00479                 << "(" << starttime.toString() << ")"
00480                 << endl;
00481     } else {
00482       variables.setFloating( true );
00483       kdDebug() << "main | parse options | "
00484                 << "Floating event time specified"
00485                 << endl;
00486     }
00487   }
00488 
00489   /*
00490    *  Set end date for calendar
00491    *
00492    */
00493   if ( args->isSet( "end-date" ) ) {
00494     QString option = args->getOption( "end-date" );
00495 
00496     kdDebug() << "main | parse options | "
00497               << "End date before conversion: "
00498               << "(" << option << ")"
00499               << endl;
00500 
00501     enddate = QDate::fromString( option,  Qt::ISODate );
00502     if ( ! enddate.isValid() ) {
00503       cout << i18n( "Invalid End Date Specified: %1" ).
00504         arg( option ).local8Bit()
00505            << endl;
00506       return 1;
00507     }
00508     kdDebug() << "main | parse options | "
00509               << "End date after conversion: "
00510               << "(" << enddate.toString() << ")"
00511               << endl;
00512   }
00513 
00514   /*
00515    *  Show next # days and exit
00516    *
00517    */
00518   if ( args->isSet( "show-next" ) )
00519   {
00520     bool ok;
00521 
00522     option = args->getOption( "show-next" );
00523     kdDebug() << "main | parse options | "
00524               << "Show " << option << " days ahead"
00525               << endl;
00526     variables.setDaysCount( option.toInt( &ok, 10 ) );
00527 
00528     if ( ! ok ) {
00529       cout << i18n( "Invalid Date Count Specified: %1" ).
00530         arg( option ).local8Bit()
00531            << endl;
00532       return 1;
00533     }
00534 
00535     enddate = startdate;
00536     enddate = enddate.addDays( variables.getDaysCount() );
00537     kdDebug() << "main | parse options | "
00538               << "End date after conversion: "
00539               << "(" << enddate.toString() << ")"
00540               << endl;
00541   }
00542 
00543   /*
00544    *  Set ending time
00545    *
00546    */
00547   if ( args->isSet( "end-time" ) ) {
00548     option = args->getOption( "end-time" );
00549 
00550     kdDebug() << "main | parse options | "
00551               << "End time before conversion: "
00552               << "(" << option << ")"
00553               << endl;
00554 
00555     if ( option.upper() != "FLOAT" ) {
00556       endtime = QTime::fromString( option,  Qt::ISODate );
00557       if ( ! endtime.isValid() ) {
00558         cout << i18n( "Invalid End Time Specified: %1" ).
00559           arg( option ).local8Bit()
00560              << endl;
00561         return 1;
00562       }
00563 
00564       kdDebug() << "main | parse options | "
00565                 << "End time after conversion: "
00566                 << "(" << endtime.toString() << ")"
00567                 << endl;
00568     } else {
00569       variables.setFloating( true );
00570       kdDebug() << "main | parse options | "
00571                 << "Floating event time specified"
00572                 << endl;
00573     }
00574   }
00575 
00576   /*
00577    *  Set start date/time from epoch
00578    *
00579    */
00580   time_t epochstart=0;
00581   if ( args->isSet( "epoch-start" ) ) {
00582     option = args->getOption( "epoch-start" );
00583 
00584     kdDebug() << "main | parse options | "
00585               << "Epoch start: "
00586               << "(" << option << ")"
00587               << endl;
00588 
00589     epochstart = (time_t) option.toULong( 0, 10 );
00590   }
00591 
00592   /*
00593    *  Set end date/time from epoch
00594    *
00595    */
00596   time_t epochend=0;
00597   if ( args->isSet( "epoch-end" ) ) {
00598     option = args->getOption( "epoch-end" );
00599 
00600     kdDebug() << "main | parse options | "
00601               << "Epoch end: "
00602               << "(" << option << ")"
00603               << endl;
00604 
00605     epochend = (time_t) option.toULong( 0, 10 );
00606   }
00607 
00608   if ( args->isSet( "all" ) ) {
00609     variables.setAll( true );
00610   } else {
00611     variables.setAll( false );
00612   }
00613 
00614   if ( args->isSet( "import" ) ) {
00615     view = false;
00616     importFile = true;
00617     option = args->getOption( "import" );
00618     variables.setImportFile( option );
00619 
00620     kdDebug() << "main | parse options | "
00621               << "importing file from: "
00622               << "(" << option << ")"
00623               << endl;
00624   }
00625 
00626   KonsoleKalendar *konsolekalendar = new KonsoleKalendar( &variables );
00627 
00628   if ( args->isSet( "file" ) ) {
00629     calendarFile = true;
00630     option = args->getOption( "file" );
00631     variables.setCalendarFile( option );
00632 
00633     /*
00634      * All modes need to know if the calendar file exists
00635      * This must be done before we get to opening biz
00636      */
00637     bool exists = QFile::exists( variables.getCalendarFile() );
00638 
00639     if ( create ) {
00640 
00641       kdDebug() << "main | createcalendar | "
00642                 << "check if calendar file already exists"
00643                 << endl;
00644 
00645       if ( exists ) {
00646         cout << i18n( "Calendar %1 already exists" ).
00647           arg( variables.getCalendarFile() ).local8Bit()
00648              << endl;
00649         return 1;
00650       }
00651       if ( konsolekalendar->createCalendar() ) {
00652         cout << i18n( "Calendar %1 successfully created" ).
00653           arg( variables.getCalendarFile() ).local8Bit()
00654              << endl;
00655         return 0;
00656       } else {
00657         cout << i18n( "Unable to create calendar: %1" ).
00658           arg( variables.getCalendarFile() ).local8Bit()
00659              << endl;
00660         return 1;
00661       }
00662     }
00663 
00664     if ( ! exists ) {
00665       cout << i18n( "Calendar file not found %1" ).
00666         arg( option ).local8Bit()
00667            << endl;
00668       cout << i18n( "Try --create to create new calendar file" ).local8Bit()
00669            << endl;
00670       return 1;
00671     }
00672   }
00673 
00674   CalendarResources *calendarResource = NULL;
00675   CalendarLocal *localCalendar = NULL;
00676 
00677   /*
00678    * Should we use local calendar or resource?
00679    */
00680   variables.setTimeZoneId();
00681   if ( args->isSet( "file" ) ) {
00682     localCalendar = new CalendarLocal( variables.getTimeZoneId() );
00683     localCalendar->load( variables.getCalendarFile() );
00684     variables.setCalendar( localCalendar  );
00685   } else {
00686     calendarResource = new CalendarResources( variables.getTimeZoneId() );
00687     calendarResource->readConfig();
00688     calendarResource->load();
00689     variables.setCalendarResources( calendarResource );
00690   }
00691 
00692   /***************************************************************************
00693    * Glorious date/time checking and setting code                            *
00694    ***************************************************************************/
00695   QDateTime startdatetime, enddatetime;
00696 
00697   // Handle case with either date or end-date unspecified
00698   if ( ! args->isSet( "end-date" ) && ! args->isSet( "show-next" ) &&
00699        args->isSet( "date" ) ) {
00700     enddate = startdate;
00701     kdDebug() << "main | datetimestamp | "
00702               << "setting enddate to startdate"
00703               << endl;
00704   } else if ( args->isSet( "end-date" ) && ! args->isSet( "date" ) ) {
00705     startdate = enddate;
00706     kdDebug() << "main | datetimestamp | "
00707               << "setting startdate to enddate"
00708               << endl;
00709   }
00710 
00711   // NOTE: If neither date nor end-date specified, then event will be today.
00712 
00713   // Case:
00714   //   End time (or epoch) unspecified, and start time (or epoch) IS specified.
00715   //   In this case, set the ending to 1 hour after starting.
00716   if ( ! args->isSet( "end-time" ) && ! args->isSet( "epoch-end" ) ) {
00717     if ( args->isSet( "time" ) ) {
00718       endtime = starttime.addSecs( 60*60 );  // end is 1 hour after start
00719       kdDebug() << "main | datetimestamp | "
00720                 << "setting endtime 1 hour after starttime"
00721                 << endl;
00722     } else if ( args->isSet( "epoch-start" ) ) {
00723       startdatetime = epochs.epoch2QDateTime( epochstart );
00724       enddatetime = startdatetime.addSecs( 60*60 );
00725       kdDebug() << "main | datetimestamp | "
00726                 << "setting endtime 1 hour after epochstart"
00727                 << endl;
00728     }
00729   }
00730 
00731   // Case:
00732   //   Time (or epoch) unspecified, and end-time (or epoch) IS specified.
00733   //   In this case, set the starting to 1 hour before ending.
00734   if ( ! args->isSet( "time" ) && ! args->isSet( "epoch-start" ) ) {
00735     if ( args->isSet( "end-time" ) ) {
00736       starttime = endtime.addSecs( -60*60 );  // start is 1 hour before end
00737       kdDebug() << "main | datetimestamp | "
00738                 << "setting starttime 1 hour before endtime"
00739                 << endl;
00740     } else if ( args->isSet( "epoch-end" ) ) {
00741       enddatetime = epochs.epoch2QDateTime( epochend );
00742       startdatetime = enddatetime.addSecs( -60*60 );
00743       kdDebug() << "main | datetimestamp | "
00744                 << "setting starttime 1 before after epochend"
00745                 << endl;
00746     }
00747   }
00748 
00749   // Case:
00750   //   Time (or epoch) unspecified, and end-time (or epoch) unspecified.
00751   if ( ! args->isSet( "time" )     && ! args->isSet( "epoch-start" ) &&
00752        ! args->isSet( "end-time" ) && ! args->isSet( "epoch-end" ) ) {
00753     // set default start date/time
00754     startdatetime = QDateTime::QDateTime( startdate, starttime );
00755     kdDebug() << "main | datetimestamp | "
00756               << "setting startdatetime from "
00757               << "default startdate (today) and starttime"
00758               << endl;
00759     // set default end date/time
00760     enddatetime = QDateTime::QDateTime( enddate, endtime );
00761     kdDebug() << "main | datetimestamp | "
00762               << "setting enddatetime from "
00763               << "default enddate (today) and endtime"
00764               << endl;
00765   }
00766 
00767   // Set startdatetime, enddatetime if still necessary
00768   if ( startdatetime.isNull() ) {
00769     startdatetime = QDateTime::QDateTime( startdate, starttime );
00770     kdDebug() << "main | datetimestamp | "
00771               << "setting startdatetime from startdate and starttime"
00772               << endl;
00773   }
00774   if ( enddatetime.isNull() ) {
00775     enddatetime = QDateTime::QDateTime( enddate, endtime );
00776     kdDebug() << "main | datetimestamp | "
00777               << "setting enddatetime from enddate and endtime"
00778               << endl;
00779   }
00780 
00781   // Float check for add mode:
00782   //   Events float if time AND end-time AND epoch times are UNspecified
00783   if ( add ) {
00784     if ( ! args->isSet( "time" )        && ! args->isSet( "end-time" ) &&
00785          ! args->isSet( "epoch-start" ) && ! args->isSet( "epoch-end" ) ) {
00786       variables.setFloating( true );
00787       kdDebug() << "main | floatingcheck | "
00788                 << "turn-on floating event"
00789                 << endl;
00790     }
00791   }
00792 
00793   // Finally! Set the start/end date times
00794   if ( ! change ) {
00795     variables.setStartDateTime( startdatetime );
00796     variables.setEndDateTime( enddatetime );
00797   } else {
00798     // Do NOT set start/end datetimes in change mode,
00799     //   unless they were specified on commandline
00800     if ( args->isSet( "time" )     || args->isSet( "epoch-start" ) ||
00801          args->isSet( "end-time" ) || args->isSet( "epoch-end" ) ) {
00802       variables.setStartDateTime( startdatetime );
00803       variables.setEndDateTime( enddatetime );
00804     }
00805   }
00806 
00807   // Some more debug prints
00808   kdDebug() << "main | datetimestamp | StartDate="
00809             << startdatetime.toString( Qt::TextDate )
00810             << endl;
00811   kdDebug() << "main | datetimestamp | EndDate="
00812             << enddatetime.toString( Qt::TextDate )
00813             << endl;
00814 
00815   /***************************************************************************
00816    * Sanity checks                                                           *
00817    ***************************************************************************/
00818 
00819   // Cannot combine modes
00820   if ( create + view + add + change + del > 1 ) {
00821     cout << i18n(
00822       "Only 1 operation mode "
00823       "(view, add, change, delete, create) "
00824       "permitted at any one time"
00825       ).local8Bit() << endl;
00826     return 1;
00827   }
00828 
00829   // Cannot have a ending before starting
00830   if ( startdatetime > enddatetime ) {
00831     cout << i18n(
00832       "Ending Date/Time occurs before the Starting Date/Time"
00833       ).local8Bit() << endl;
00834     return 1;
00835   }
00836 
00837   /***************************************************************************
00838    * And away we go with the real work...                                    *
00839    ***************************************************************************/
00840 
00841   args->clear(); // Free up some memory.
00842 
00843   /*
00844    * Set our application name for use in unique IDs and error messages,
00845    * and product ID for incidence PRODID property
00846    */
00847   QString prodId = "-//K Desktop Environment//NONSGML %1 %2//EN";
00848   CalFormat::setApplication( progDisplay,
00849                              prodId.arg( progDisplay).arg( progVersion ) );
00850 
00851   if ( importFile ) {
00852     if ( konsolekalendar->importCalendar() ) {
00853       cout << i18n( "Calendar %1 successfully imported" ).
00854         arg( variables.getImportFile() ).local8Bit()
00855            << endl;
00856       return 0;
00857     } else {
00858       cout << i18n( "Unable to import calendar: %1" ).
00859         arg( variables.getImportFile() ).local8Bit()
00860            << endl;
00861       return 1;
00862     }
00863   }
00864 
00865   if ( add ) {
00866     if ( ! konsolekalendar->isEvent( startdatetime, enddatetime,
00867                                      variables.getSummary() ) ) {
00868       kdDebug() << "main | modework | "
00869                 << "calling addEvent()"
00870                 << endl;
00871       konsolekalendar->addEvent();
00872     } else {
00873       cout << i18n(
00874         "Attempting to insert an event that already exists"
00875         ).local8Bit() << endl;
00876       return 1;
00877     }
00878   }
00879 
00880   if ( change ) {
00881     kdDebug() << "main | modework | "
00882               << "calling changeEvent()"
00883               << endl;
00884     if ( ! variables.isUID() ) {
00885       cout << i18n( "Missing event UID: "
00886                     "use --uid command line option" ).local8Bit()
00887            << endl;
00888       return 1;
00889     }
00890     if ( konsolekalendar->changeEvent() != true ) {
00891       cout << i18n( "No such event UID: change event failed" ).local8Bit()
00892            << endl;
00893       return 1;
00894     }
00895     kdDebug() << "main | modework | "
00896               << "successful changeEvent()"
00897               << endl;
00898   }
00899 
00900   if ( del ) {
00901     kdDebug() << "main | modework | "
00902               << "calling deleteEvent()"
00903               << endl;
00904     if ( ! variables.isUID() ) {
00905       cout << i18n( "Missing event UID: "
00906                     "use --uid command line option" ).local8Bit()
00907            << endl;
00908       return 1;
00909     }
00910     if ( konsolekalendar->deleteEvent() != true ) {
00911       cout << i18n( "No such event UID: delete event failed").local8Bit()
00912            << endl;
00913       return 1;
00914     }
00915     kdDebug() << "main | modework | "
00916               << "successful deleteEvent()"
00917               << endl;
00918   }
00919 
00920   if ( view ) {
00921     kdDebug() << "main | modework | "
00922               << "calling showInstance() to view events"
00923               << endl;
00924     if ( ! konsolekalendar->showInstance() ) {
00925       cout << i18n( "Cannot open specified export file: %1" ).
00926         arg( variables.getExportFile() ).local8Bit()
00927            << endl;
00928       return 1;
00929     }
00930   }
00931 
00932   delete konsolekalendar;
00933 
00934   if ( calendarFile ) {
00935     localCalendar->close();
00936     delete localCalendar;
00937   } else {
00938     calendarResource->close();
00939     delete calendarResource;
00940   }
00941 
00942   kdDebug() << "main | exiting"
00943             << endl;
00944 
00945   return 0;
00946 }
KDE Logo
This file is part of the documentation for konsolekalendar Library Version 3.3.2.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Thu May 3 20:25:55 2007 by doxygen 1.4.2 written by Dimitri van Heesch, © 1997-2003