summaryrefslogtreecommitdiffstats
path: root/konsolekalendar/main.cpp
blob: 60b45dde3aae7765832f067e1be8dd61e2618c30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
/*******************************************************************************
 * main.cpp                                                                    *
 *                                                                             *
 * KonsoleKalendar is a command line interface to KDE calendars                *
 * Copyright (C) 2002-2004  Tuukka Pasanen <illuusio@mailcity.com>             *
 * Copyright (C) 2003-2005  Allen Winter <winter@kde.org>                      *
 *                                                                             *
 * This program is free software; you can redistribute it and/or modify        *
 * it under the terms of the GNU General Public License as published by        *
 * the Free Software Foundation; either version 2 of the License, or           *
 * (at your option) any later version.                                         *
 *                                                                             *
 * This program is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the                *
 * GNU General Public License for more details.                                *
 *                                                                             *
 * You should have received a copy of the GNU General Public License           *
 * along with this program; if not, write to the Free Software                 *
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
 *                                                                             *
 * As a special exception, permission is given to link this program            *
 * with any edition of TQt, and distribute the resulting executable,            *
 * without including the source code for TQt in the source distribution.        *
 *                                                                             *
 ******************************************************************************/
/**
 * @file main.cpp
 * KonsoleKalendar main program.
 * @author Tuukka Pasanen
 * @author Allen Winter
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#if TIME_WITH_SYS_TIME
# include <sys/time.h>
# include <time.h>
#else
# if HAVE_SYS_TIME_H
#  include <sys/time.h>
# else
#  include <time.h>
# endif
#endif

#include <kcmdlineargs.h>
#include <kaboutdata.h>
#include <klocale.h>
#include <kglobal.h>
#include <kconfig.h>
#include <kstandarddirs.h>
#include <kdebug.h>

#include <libkcal/calformat.h>
#include <libkcal/calendarresources.h>
#include <libkcal/resourcelocal.h>

#include <tqdatetime.h>
#include <tqfile.h>
#include <tqfileinfo.h>

#include <stdlib.h>
#include <iostream>

#include "stdcalendar.h"
#include "konsolekalendar.h"
#include "konsolekalendarepoch.h"

#include "konsolekalendarvariables.h"

using namespace KCal;
using namespace std;

static const char progName[] = "konsolekalendar";
static const char progDisplay[] = "KonsoleKalendar";
static const char progVersion[] = "1.3.5";
static const char progDesc[] = "A command line interface to KDE calendars";
static const char progURL[] = "pim.kde.org/components/konsolekalendar.php";


static KCmdLineOptions options[] =
{
  { "verbose",
    I18N_NOOP( "Print helpful runtime messages" ), 0 },
  { "dry-run",
    I18N_NOOP( "Print what would have been done, but do not execute" ), 0 },
  { "file <calendar-file>",
    I18N_NOOP( "Specify which calendar you want to use" ), 0 },

  { ":",
    I18N_NOOP( "Incidence types (these options can be combined):" ), 0 },
  { "event",
    I18N_NOOP( "  Operate for Events only (Default)" ), 0 },
  { "todo",
    I18N_NOOP( "  Operate for To-dos only [NOT WORKING YET]" ), 0 },
  { "journal",
    I18N_NOOP( "  Operate for Journals only [NOT WORKING YET]" ), 0 },

  { ":",
    I18N_NOOP( "Major operation modes:" ), 0 },
  { "view",
    I18N_NOOP( "  Print incidences in specified export format" ), 0 },
  { "add",
    I18N_NOOP( "  Insert an incidence into the calendar" ), 0 },
  { "change",
    I18N_NOOP( "  Modify an existing incidence" ), 0 },
  { "delete",
    I18N_NOOP( "  Remove an existing incidence" ), 0 },
  { "create",
    I18N_NOOP( "  Create new calendar file if one does not exist" ), 0 },
  { "import <import-file>",
    I18N_NOOP( "  Import this calendar to main calendar" ), 0 },
  { ":",
    I18N_NOOP( "Operation modifiers:" ), 0 },
  { "all",
    I18N_NOOP( "  View all calendar entries" ), 0 },
  { "next",
    I18N_NOOP( "  View next activity in calendar" ), 0 },
  { "show-next <days>",
    I18N_NOOP( "  From start date show next # days' activities" ), 0 },
  { "uid <uid>",
    I18N_NOOP( "  Incidence Unique-string identifier" ), 0 },
  { "date <start-date>",
    I18N_NOOP( "  Start from this day [YYYY-MM-DD]" ), 0 },
  { "time <start-time>",
    I18N_NOOP( "  Start from this time [HH:MM:SS]" ), 0 },
  { "end-date <end-date>",
    I18N_NOOP( "  End at this day [YYYY-MM-DD]" ), 0 },
  { "end-time <end-time>",
    I18N_NOOP( "  End at this time [HH:MM:SS]" ), 0 },
  { "epoch-start <epoch-time>",
    I18N_NOOP( " Start from this time [secs since epoch]" ), 0 },
  { "epoch-end <epoch-time>",
    I18N_NOOP( "  End at this time [secs since epoch]" ), 0 },
  { "summary <summary>",
    I18N_NOOP( "  Add summary to incidence (for add/change modes)" ), 0 },
  { "description <description>",
    I18N_NOOP( "Add description to incidence (for add/change modes)" ), 0 },
  { "location <location>",
    I18N_NOOP( "  Add location to incidence (for add/change modes)" ), 0 },

  { ":", I18N_NOOP( "Export options:" ), 0 },
  { "export-type <export-type>",
    I18N_NOOP( "Export file type (Default: text)" ), 0 },
  { "export-file <export-file>",
    I18N_NOOP( "Export to file (Default: stdout)" ), 0 },
  { "export-list",
    I18N_NOOP( "  Print list of export types supported and exit" ), 0 },

  { "",
    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-1740326.803" ), 0 },

  { "",
    I18N_NOOP( "For more information visit the program home page at:\n"
               "  http://pim.kde.org/components/konsolekalendar.php" ), 0 },

  KCmdLineLastOption
};

int main( int argc, char *argv[] )
{
  KAboutData aboutData(
    progName,                        // internal program name
    I18N_NOOP( progDisplay ),        // displayable program name.
    progVersion,                     // version string
    I18N_NOOP( progDesc ),           // short porgram description
    KAboutData::License_GPL,         // license type
    "(c) 2002-2005, Tuukka Pasanen and Allen Winter", // copyright statement
    0,                               // any free form text
    progURL,                         // program home page address
    "bugs.kde.org"                   // bug report email address
    );

  aboutData.addAuthor(
    "Tuukka Pasanen",                // developer's name
    I18N_NOOP( "Primary Author" ),   // task or role
    "illuusio@mailcity.com",         // email address
    0                                // home page or relevant link
    );
  aboutData.addAuthor(
    "Allen Winter",                  // developer's name
    I18N_NOOP( "Author" ),           // task or role
    "winter@kde.org",                // email address
    0                                // home page or relevant link
    );

  //TODO: KDE 4.0, add the StdCmdLineArgs option to init()
  TDECmdLineArgs::init( argc, argv, &aboutData );
  TDECmdLineArgs::addCmdLineOptions( options ); // Add our own options.

  KApplication app(
    false, //do not allowstyles - disable the loading on plugin based styles
    false  //GUI is not enabled - disable all GUI stuff
    );

  TDECmdLineArgs *args = TDECmdLineArgs::parsedArgs();

  // Default values for start date/time (today at 07:00)
  TQDate startdate = TQDate::currentDate();
  TQTime starttime( 7, 0 );

  // Default values for end date/time (today at 17:00)
  TQDate enddate = TQDate::currentDate();
  TQTime endtime( 17, 0 );

  // Default values for switches
  bool view = true;
  bool add = false;
  bool change = false;
  bool del = false;
  bool create = false;
  bool calendarFile = false;
  bool importFile = false;

  TQString option;

  KonsoleKalendarVariables variables;
  KonsoleKalendarEpoch epochs;

  variables.setFloating( false ); // by default, new events do NOT float

  if ( args->isSet( "verbose" ) ) {
    variables.setVerbose( true );
  }

  if ( args->isSet( "dry-run" ) ) {
    variables.setDryRun( true );
  }

  /*
   * Switch on export list
   */
  if ( args->isSet( "export-list" ) ) {
    cout << endl;
    cout << i18n( "%1 supports these export formats:" ).
      arg( progDisplay ).local8Bit().data()
         << endl;
    cout << i18n( "  %1 [Default]" ).
      arg( "Text" ).local8Bit().data()
         << endl;
    cout << i18n( "  %1 (like %2, but more compact)" ).
      arg( "Short", "Text" ).local8Bit().data()
         << endl;
    cout << i18n( "  %1" ).
      arg( "HTML" ).local8Bit().data()
         << endl;
    cout << i18n( "  %1 (like %2, but in a month view)" ).
      arg( "HTMLmonth", "HTML" ).local8Bit().data()
         << endl;
    cout << i18n( "  %1 (Comma-Separated Values)" ).
      arg( "CSV" ).local8Bit().data()
         << endl;
    cout << endl;
    return 0;
  }

  /*
   * Set incidence type(s)
   */
  if ( args->isSet( "event" ) ) {
    variables.setUseEvents( true );
    kdDebug() << "main | parse options | use Events" << endl;
  }
  if ( args->isSet( "todo" ) ) {
    variables.setUseTodos( true );
    kdDebug() << "main | parse options | use To-dos" << endl;
    cout << i18n( "Sorry, To-dos are not working yet." ).local8Bit().data()
         << endl;
    return 1;
  }
  if ( args->isSet( "journal" ) ) {
    variables.setUseJournals( true );
    kdDebug() << "main | parse options | use Journals" << endl;
    cout << i18n( "Sorry, Journals are not working yet." ).local8Bit().data()
         << endl;
    return 1;
  }
  // Use Events if no incidence type is specified on the command line
  if ( !args->isSet( "event" ) &&
       !args->isSet( "todo" ) &&
       !args->isSet( "journal" ) ) {
    variables.setUseEvents( true );
    kdDebug() << "main | parse options | use Events (Default)" << endl;
  }

  /*
   * Switch on exporting
   */
  variables.setExportType( ExportTypeText );
  if ( args->isSet( "export-type" ) ) {
    option = args->getOption( "export-type" );

    if ( option.upper() == "HTML" ) {
      kdDebug() << "main | export-type | Export to HTML" << endl;
      variables.setExportType( ExportTypeHTML );
    } else if ( option.upper() == "HTMLMONTH" ) {
      kdDebug() << "main | export-type | Export to HTML by Month" << endl;
      variables.setExportType( ExportTypeMonthHTML );
    } else if ( option.upper() == "CSV" ) {
      kdDebug() << "main | export-type | Export to CSV" << endl;
      variables.setExportType( ExportTypeCSV );
    } else if ( option.upper() == "TEXT" ) {
      kdDebug() << "main | export-type | Export to TEXT (default)" << endl;
      variables.setExportType( ExportTypeText );
    } else if ( option.upper() == "SHORT" ) {
      kdDebug() << "main | export-type | Export to TEXT-SHORT" << endl;
      variables.setExportType( ExportTypeTextShort );
    } else {
      cout << i18n( "Invalid Export Type Specified: %1" ).
        arg( option ).local8Bit().data()
           << endl;
      return 1;
    }
  }

  /*
   * Switch on export file name
   */
  if ( args->isSet( "export-file" ) ) {
    option = args->getOption( "export-file" );

    kdDebug() << "main | parse options | "
              << "Export File: "
              << "(" << option << ")"
              << endl;

    variables.setExportFile( option );
  }

  /*
   * Switch on View (Print Entries).  This is the default mode of operation.
   */
  if ( args->isSet( "view" ) ) {
    view = true;

    kdDebug() << "main | parse options | "
              << "Mode: (Print incidences)"
              << endl;
  }

  /*
   * Switch on Add (Insert Entry)
   */
  if ( args->isSet( "add" ) ) {
    view = false;
    add = true;

    kdDebug() << "main | parse options | "
              << "Mode: (Add incidence)"
              << endl;
  }

  /*
   * Switch on Change (Modify Entry)
   */
  if ( args->isSet( "change" ) ) {
    view = false;
    change = true;

    kdDebug() << "main | parse options | "
              << "Mode: (Change incidence)"
              << endl;
  }

  /*
   * Switch on Delete (Remove Entry)
   */
  if ( args->isSet( "delete" ) ) {
    view = false;
    del = true;

    kdDebug() << "main | parse options | "
              << "Mode: (Delete incidence)"
              << endl;
  }

  /*
   * Switch on Create
   */
  if ( args->isSet( "create" ) ) {
    view = false;
    create = true;

    kdDebug() << "main | parse options | "
              << "Calendar File: (Create)"
              << endl;
  }

  /*
   * If there is summary attached.
   */
  if ( args->isSet( "summary" ) ) {
    option = args->getOption( "summary" );

    kdDebug() << "main | parse options | "
              << "Summary: "
              << "(" << option << ")"
              << endl;

    variables.setSummary( option );
  }

  /*
   * If there is description attached.
   */
  if ( args->isSet( "description" ) ) {
    option = args->getOption( "description" );

    kdDebug() << "main | parse options | "
              << "Description: "
              << "(" << option << ")"
              << endl;

    variables.setDescription( option );
  }

  /*
   * If there is location information
   */
  if ( args->isSet( "location" ) ) {
    option = args->getOption( "location" );

    kdDebug() << "main | parse options | "
              << "Location: "
              << "(" << option << ")"
              << endl;

    variables.setLocation( option );
  }

  /*
   * Show next happening and exit
   */
  if ( args->isSet( "next" ) ) {
    kdDebug() << "main | parse options | "
              << "Show next incidence only"
              << endl;

    variables.setNext( true );
  }

  /*
   * Set incidence unique string identifier
   */
  if ( args->isSet( "uid" ) ) {
    option = args->getOption( "uid" );

    kdDebug() << "main | parse options | "
              << "incidence UID: "
              << "(" << option << ")"
              << endl;

    variables.setUID( option );
  }

  /*
   * Set starting date for calendar
   */
  if ( args->isSet( "date" ) ) {
    option = args->getOption( "date" );

    kdDebug() << "main | parse options | "
              << "Start date before conversion: "
              << "(" << option << ")"
              << endl;

    startdate = TQDate::fromString( option, Qt::ISODate );
    if ( !startdate.isValid() ) {
      cout << i18n( "Invalid Start Date Specified: %1" ).
        arg( option ).local8Bit().data()
           << endl;
      return 1;
    }
    kdDebug() << "main | parse options | "
              << "Start date after conversion: "
              << "(" << TQString(startdate.toString()) << ")"
              << endl;
  }

  /*
   * Set starting time
   */
  if ( args->isSet( "time" ) ) {
    option = args->getOption( "time" );

    kdDebug() << "main | parse options | "
              << "Start time before conversion : "
              << "(" << option << ")"
              << endl;

    if ( option.upper() != "FLOAT" ) {
      starttime = TQTime::fromString( option, Qt::ISODate );
      if ( !starttime.isValid() ) {
        cout << i18n( "Invalid Start Time Specified: %1" ).
          arg( option ).local8Bit().data()
             << endl;
        return 1;
      }
      kdDebug() << "main | parse options | "
                << "Start time after conversion: "
                << "(" << TQString(starttime.toString()) << ")"
                << endl;
    } else {
      variables.setFloating( true );
      kdDebug() << "main | parse options | "
                << "Floating event time specified"
                << endl;
    }
  }

  /*
   * Set end date for calendar
   */
  if ( args->isSet( "end-date" ) ) {
    option = args->getOption( "end-date" );

    kdDebug() << "main | parse options | "
              << "End date before conversion: "
              << "(" << option << ")"
              << endl;

    enddate = TQDate::fromString( option, Qt::ISODate );
    if ( !enddate.isValid() ) {
      cout << i18n( "Invalid End Date Specified: %1" ).
        arg( option ).local8Bit().data()
           << endl;
      return 1;
    }
    kdDebug() << "main | parse options | "
              << "End date after conversion: "
              << "(" << TQString(enddate.toString()) << ")"
              << endl;
  }

  /*
   * Show next # days and exit
   */
  if ( args->isSet( "show-next" ) ) {
    bool ok;

    option = args->getOption( "show-next" );
    kdDebug() << "main | parse options | "
              << "Show " << option << " days ahead"
              << endl;
    variables.setDaysCount( option.toInt( &ok, 10 ) );

    if ( !ok ) {
      cout << i18n( "Invalid Date Count Specified: %1" ).
        arg( option ).local8Bit().data()
           << endl;
      return 1;
    }

    enddate = startdate;
    enddate = enddate.addDays( variables.getDaysCount() );
    kdDebug() << "main | parse options | "
              << "End date after conversion: "
              << "(" << TQString(enddate.toString()) << ")"
              << endl;
  }

  /*
   * Set ending time
   */
  if ( args->isSet( "end-time" ) ) {
    option = args->getOption( "end-time" );

    kdDebug() << "main | parse options | "
              << "End time before conversion: "
              << "(" << option << ")"
              << endl;

    if ( option.upper() != "FLOAT" ) {
      endtime = TQTime::fromString( option, Qt::ISODate );
      if ( !endtime.isValid() ) {
        cout << i18n( "Invalid End Time Specified: %1" ).
          arg( option ).local8Bit().data()
             << endl;
        return 1;
      }

      kdDebug() << "main | parse options | "
                << "End time after conversion: "
                << "(" << TQString(endtime.toString()) << ")"
                << endl;
    } else {
      variables.setFloating( true );
      kdDebug() << "main | parse options | "
                << "Floating event time specified"
                << endl;
    }
  }

  /*
   * Set start date/time from epoch
   */
  time_t epochstart = 0;
  if ( args->isSet( "epoch-start" ) ) {
    option = args->getOption( "epoch-start" );

    kdDebug() << "main | parse options | "
              << "Epoch start: "
              << "(" << option << ")"
              << endl;

    epochstart = ( time_t ) option.toULong( 0, 10 );
  }

  /*
   * Set end date/time from epoch
   */
  time_t epochend = 0;
  if ( args->isSet( "epoch-end" ) ) {
    option = args->getOption( "epoch-end" );

    kdDebug() << "main | parse options | "
              << "Epoch end: "
              << "(" << option << ")"
              << endl;

    epochend = ( time_t ) option.toULong( 0, 10 );
  }

  if ( args->isSet( "all" ) ) {
    variables.setAll( true );
  } else {
    variables.setAll( false );
  }

  if ( args->isSet( "import" ) ) {
    view = false;
    importFile = true;
    option = args->getOption( "import" );
    variables.setImportFile( option );

    kdDebug() << "main | parse options | "
              << "importing file from: "
              << "(" << option << ")"
              << endl;
  }

  KonsoleKalendar *konsolekalendar = new KonsoleKalendar( &variables );

  if ( args->isSet( "file" ) ) {
    calendarFile = true;
    option = args->getOption( "file" );
    variables.setCalendarFile( option );

    /*
     * All modes need to know if the calendar file exists
     * This must be done before we get to opening biz
     */
    bool exists, remote;
    KURL url = KURL::fromPathOrURL( variables.getCalendarFile() );
    if ( url.isLocalFile() ) {
      variables.setCalendarFile( url.path() );
      exists = TQFile::exists( variables.getCalendarFile() );
      remote = false;
    } else if ( url.protocol().isEmpty() ) {
      TQFileInfo info( variables.getCalendarFile() );
      variables.setCalendarFile( info.absFilePath() );
      exists = TQFile::exists( variables.getCalendarFile() );
      remote = false;
    } else {
      exists = true; // really have no idea if the remote file exists
      remote = true;
    }

    if ( create ) {

      kdDebug() << "main | createcalendar | "
                << "check if calendar file already exists"
                << endl;

      if ( remote ) {
        cout << i18n( "Attempting to create a remote file %1" ).
          arg( variables.getCalendarFile() ).local8Bit().data() << endl;
        return 1;
      } else {
        if ( exists ) {
          cout << i18n( "Calendar %1 already exists" ).
            arg( variables.getCalendarFile() ).local8Bit().data()
               << endl;
          return 1;
        }
      }

      if ( konsolekalendar->createCalendar() ) {
        cout << i18n( "Calendar %1 successfully created" ).
          arg( variables.getCalendarFile() ).local8Bit().data()
             << endl;
        return 0;
      } else {
        cout << i18n( "Unable to create calendar: %1" ).
          arg( variables.getCalendarFile() ).local8Bit().data()
             << endl;
        return 1;
      }
    }

    if ( !exists ) {
      cout << i18n( "Calendar file not found %1" ).
        arg( variables.getCalendarFile() ).local8Bit().data()
           << endl;
      cout << i18n( "Try --create to create new calendar file" ).local8Bit().data()
           << endl;
      return 1;
    }
  }

  CalendarResources *calendarResource = NULL;
  /*
   * Should we use local calendar or resource?
   */
  if ( args->isSet( "file" ) ) {
    calendarResource = new StdCalendar( variables.getCalendarFile(),
                                        i18n( "Active Calendar" ) );
  } else {
    // TODO: when certain resources (kolab) don't try to gain access to
    // an X server, or dcopserver, then put back the following line which
    // supports all resources, not just the standard resource.
    // calendarResource = new StdCalendar();
    calendarResource = new StdCalendar( locateLocal( "data",
                                                     "korganizer/std.ics" ),
                                        i18n( "Default Calendar" ) );
  }
  if ( !args->isSet( "import" ) ) {
    variables.setCalendar( calendarResource );
    calendarResource->load();
  }

  /***************************************************************************
   * Glorious date/time checking and setting code                            *
   ***************************************************************************/
  TQDateTime startdatetime, enddatetime;

  // Handle case with either date or end-date unspecified
  if ( !args->isSet( "end-date" ) && !args->isSet( "show-next" ) &&
       args->isSet( "date" ) ) {
    enddate = startdate;
    kdDebug() << "main | datetimestamp | "
              << "setting enddate to startdate"
              << endl;
  } else if ( args->isSet( "end-date" ) && !args->isSet( "date" ) ) {
    startdate = enddate;
    kdDebug() << "main | datetimestamp | "
              << "setting startdate to enddate"
              << endl;
  }

  // NOTE: If neither date nor end-date specified, then event will be today.

  // Case:
  //   End time (or epoch) unspecified, and start time (or epoch) IS specified.
  //   In this case, set the ending to 1 hour after starting.
  if ( !args->isSet( "end-time" ) && !args->isSet( "epoch-end" ) ) {
    if ( args->isSet( "time" ) ) {
      endtime = starttime.addSecs( 60 * 60 );  // end is 1 hour after start
      kdDebug() << "main | datetimestamp | "
                << "setting endtime 1 hour after starttime"
                << endl;
    } else if ( args->isSet( "epoch-start" ) ) {
      startdatetime = epochs.epoch2TQDateTime( epochstart );
      enddatetime = startdatetime.addSecs( 60 * 60 );
      kdDebug() << "main | datetimestamp | "
                << "setting endtime 1 hour after epochstart"
                << endl;
    }
  }

  // Case:
  //   Time (or epoch) unspecified, and end-time (or epoch) IS specified.
  //   In this case, set the starting to 1 hour before ending.
  if ( !args->isSet( "time" ) && !args->isSet( "epoch-start" ) ) {
    if ( args->isSet( "end-time" ) ) {
      starttime = endtime.addSecs( -60 * 60 );  // start is 1 hour before end
      kdDebug() << "main | datetimestamp | "
                << "setting starttime 1 hour before endtime"
                << endl;
    } else if ( args->isSet( "epoch-end" ) ) {
      enddatetime = epochs.epoch2TQDateTime( epochend );
      startdatetime = enddatetime.addSecs( -60 * 60 );
      kdDebug() << "main | datetimestamp | "
                << "setting starttime 1 before after epochend"
                << endl;
    }
  }

  // Case:
  //   Time (or epoch) unspecified, and end-time (or epoch) unspecified.
  if ( !args->isSet( "time" )     && !args->isSet( "epoch-start" ) &&
       !args->isSet( "end-time" ) && !args->isSet( "epoch-end" ) ) {
    // set default start date/time
    startdatetime = TQDateTime( startdate, starttime );
    kdDebug() << "main | datetimestamp | "
              << "setting startdatetime from "
              << "default startdate (today) and starttime"
              << endl;
    // set default end date/time
    enddatetime = TQDateTime( enddate, endtime );
    kdDebug() << "main | datetimestamp | "
              << "setting enddatetime from "
              << "default enddate (today) and endtime"
              << endl;
  }

  // Set startdatetime, enddatetime if still necessary
  if ( startdatetime.isNull() ) {
    startdatetime = TQDateTime( startdate, starttime );
    kdDebug() << "main | datetimestamp | "
              << "setting startdatetime from startdate and starttime"
              << endl;
  }
  if ( enddatetime.isNull() ) {
    enddatetime = TQDateTime( enddate, endtime );
    kdDebug() << "main | datetimestamp | "
              << "setting enddatetime from enddate and endtime"
              << endl;
  }

  // Float check for add mode:
  //   Events float if time AND end-time AND epoch times are UNspecified
  if ( add ) {
    if ( !args->isSet( "time" )        && !args->isSet( "end-time" ) &&
         !args->isSet( "epoch-start" ) && !args->isSet( "epoch-end" ) ) {
      variables.setFloating( true );
      kdDebug() << "main | floatingcheck | "
                << "turn-on floating event"
                << endl;
    }
  }

  // Finally! Set the start/end date times
  if ( !change ) {
    variables.setStartDateTime( startdatetime );
    variables.setEndDateTime( enddatetime );
  } else {
    // Do NOT set start/end datetimes in change mode,
    //   unless they were specified on commandline
    if ( args->isSet( "time" )     || args->isSet( "epoch-start" ) ||
         args->isSet( "end-time" ) || args->isSet( "epoch-end" ) ) {
      variables.setStartDateTime( startdatetime );
      variables.setEndDateTime( enddatetime );
    }
  }

  // Some more debug prints
  kdDebug() << "main | datetimestamp | StartDate="
            << startdatetime.toString( Qt::TextDate )
            << endl;
  kdDebug() << "main | datetimestamp | EndDate="
            << enddatetime.toString( Qt::TextDate )
            << endl;

  /***************************************************************************
   * Sanity checks                                                           *
   ***************************************************************************/

  // Cannot combine modes
  if ( create + view + add + change + del > 1 ) {
    cout << i18n(
      "Only 1 operation mode "
      "(view, add, change, delete, create) "
      "permitted at any one time"
      ).local8Bit().data() << endl;
    return 1;
  }

  // Cannot have a ending before starting
  if ( startdatetime > enddatetime ) {
    cout << i18n(
      "Ending Date/Time occurs before the Starting Date/Time"
      ).local8Bit().data() << endl;
    return 1;
  }

  /***************************************************************************
   * And away we go with the real work...                                    *
   ***************************************************************************/

  args->clear(); // Free up some memory.

  /*
   * Set our application name for use in unique IDs and error messages,
   * and product ID for incidence PRODID property
   */
  TQString prodId = "-//K Desktop Environment//NONSGML %1 %2//EN";
  CalFormat::setApplication( progDisplay,
                             prodId.arg( progDisplay ).arg( progVersion ) );

  if ( importFile ) {
    if ( konsolekalendar->importCalendar() ) {
      cout << i18n( "Calendar %1 successfully imported" ).
        arg( variables.getImportFile() ).local8Bit().data()
           << endl;
      return 0;
    } else {
      cout << i18n( "Unable to import calendar: %1" ).
        arg( variables.getImportFile() ).local8Bit().data()
           << endl;
      return 1;
    }
  }

  if ( add ) {
    if ( !konsolekalendar->isEvent( startdatetime, enddatetime,
                                     variables.getSummary() ) ) {
      kdDebug() << "main | modework | "
                << "calling addEvent()"
                << endl;
      konsolekalendar->addEvent();
    } else {
      cout << i18n(
        "Attempting to insert an event that already exists"
        ).local8Bit().data() << endl;
      return 1;
    }
  }

  if ( change ) {
    kdDebug() << "main | modework | "
              << "calling changeEvent()"
              << endl;
    if ( !variables.isUID() ) {
      cout << i18n( "Missing event UID: "
                    "use --uid command line option" ).local8Bit().data()
           << endl;
      return 1;
    }
    if ( konsolekalendar->changeEvent() != true ) {
      cout << i18n( "No such event UID: change event failed" ).local8Bit().data()
           << endl;
      return 1;
    }
    kdDebug() << "main | modework | "
              << "successful changeEvent()"
              << endl;
  }

  if ( del ) {
    kdDebug() << "main | modework | "
              << "calling deleteEvent()"
              << endl;
    if ( !variables.isUID() ) {
      cout << i18n( "Missing event UID: "
                    "use --uid command line option" ).local8Bit().data()
           << endl;
      return 1;
    }
    if ( konsolekalendar->deleteEvent() != true ) {
      cout << i18n( "No such event UID: delete event failed").local8Bit().data()
           << endl;
      return 1;
    }
    kdDebug() << "main | modework | "
              << "successful deleteEvent()"
              << endl;
  }

  if ( view ) {
    kdDebug() << "main | modework | "
              << "calling showInstance() to view events"
              << endl;
    if ( !konsolekalendar->showInstance() ) {
      cout << i18n( "Cannot open specified export file: %1" ).
        arg( variables.getExportFile() ).local8Bit().data()
           << endl;
      return 1;
    }
  }

  delete konsolekalendar;

  calendarResource->close();
  delete calendarResource;

  kdDebug() << "main | exiting"
            << endl;

  return 0;
}