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
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
|
/*
* This file only:
* Copyright (C) 2003, 2004 Mark Bucciarelli <[email protected]>
*
* 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.
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cassert>
#include <tqfile.h>
#include <tqsize.h>
#include <tqdict.h>
#include <tqdatetime.h>
#include <tqstring.h>
#include <tqstringlist.h>
#include "incidence.h"
#include "kapplication.h" // kapp
#include <kdebug.h>
#include <kemailsettings.h>
#include <klocale.h> // i18n
#include <kmessagebox.h>
#include <kprogress.h>
#include <ktempfile.h>
#include <resourcecalendar.h>
#include <resourcelocal.h>
#include <resourceremote.h>
#include <kpimprefs.h>
#include <taskview.h>
#include <timekard.h>
#include <karmutility.h>
#include <kio/netaccess.h>
#include <kurl.h>
#include <vector>
//#include <calendarlocal.h>
//#include <journal.h>
//#include <event.h>
//#include <todo.h>
#include "karmstorage.h"
#include "preferences.h"
#include "task.h"
#include "reportcriteria.h"
using namespace std;
KarmStorage *KarmStorage::_instance = 0;
static long linenr; // how many lines written by printTaskHistory so far
KarmStorage *KarmStorage::instance()
{
if (_instance == 0) _instance = new KarmStorage();
return _instance;
}
KarmStorage::KarmStorage()
{
_calendar = 0;
}
TQString KarmStorage::load (TaskView* view, const Preferences* preferences, TQString fileName )
// loads data from filename into view. If no filename is given, filename from preferences is used.
// filename might be of use if this program is run as embedded konqueror plugin.
{
// When I tried raising an exception from this method, the compiler
// complained that exceptions are not allowed. Not sure how apps
// typically handle error conditions in KDE, but I'll return the error
// as a string (empty is no error). -- Mark, Aug 8, 2003
// Use KDE_CXXFLAGS=$(USE_EXCEPTIONS) in Makefile.am if you want to use
// exceptions (David Faure)
TQString err;
KEMailSettings settings;
if ( fileName.isEmpty() ) fileName = preferences->iCalFile();
// If same file, don't reload
if ( fileName == _icalfile ) return err;
// If file doesn't exist, create a blank one to avoid ResourceLocal load
// error. We make it user and group read/write, others read. This is
// masked by the users umask. (See man creat)
if ( ! remoteResource( _icalfile ) )
{
int handle;
handle = open (
TQFile::encodeName( fileName ),
O_CREAT|O_EXCL|O_WRONLY,
S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH
);
if (handle != -1) close(handle);
}
if ( _calendar)
closeStorage(view);
// Create local file resource and add to resources
_icalfile = fileName;
KCal::ResourceCached *resource;
if ( remoteResource( _icalfile ) )
{
KURL url( _icalfile );
resource = new KCal::ResourceRemote( url, url ); // same url for upload and download
}
else
{
resource = new KCal::ResourceLocal( _icalfile );
}
_calendar = resource;
TQObject::connect (_calendar, TQT_SIGNAL(resourceChanged(ResourceCalendar *)),
view, TQT_SLOT(iCalFileModified(ResourceCalendar *)));
_calendar->setTimeZoneId( KPimPrefs::timezone() );
_calendar->setResourceName( TQString::tqfromLatin1("KArm") );
_calendar->open();
_calendar->load();
// Claim ownership of iCalendar file if no one else has.
KCal::Person owner = resource->getOwner();
if ( owner.isEmpty() )
{
resource->setOwner( KCal::Person(
settings.getSetting( KEMailSettings::RealName ),
settings.getSetting( KEMailSettings::EmailAddress ) ) );
}
// Build task view from iCal data
if (!err)
{
KCal::Todo::List todoList;
KCal::Todo::List::ConstIterator todo;
TQDict< Task > map;
// Build dictionary to look up Task object from Todo uid. Each task is a
// TQListViewItem, and is initially added with the view as the tqparent.
todoList = _calendar->rawTodos();
kdDebug(5970) << "KarmStorage::load "
<< "rawTodo count (includes completed todos) ="
<< todoList.count() << endl;
for( todo = todoList.begin(); todo != todoList.end(); ++todo )
{
// Initially, if a task was complete, it was removed from the view.
// However, this increased the complexity of reporting on task history.
//
// For example, if a task is complete yet has time logged to it during
// the date range specified on the history report, we have to figure out
// how that task fits into the task hierarchy. Currently, this
// structure is held in memory by the structure in the list view.
//
// I considered creating a second tree that held the full structure of
// all complete and incomplete tasks. But this seemed to much of a
// change with an impending beta release and a full todo list.
//
// Hence this "solution". Include completed tasks, but mark them as
// inactive in the view.
//
//if ((*todo)->isCompleted()) continue;
Task* task = new Task(*todo, view);
map.insert( (*todo)->uid(), task );
view->setRootIsDecorated(true);
task->setPixmapProgress();
}
// Load each task under it's tqparent task.
for( todo = todoList.begin(); todo != todoList.end(); ++todo )
{
Task* task = map.tqfind( (*todo)->uid() );
// No relatedTo incident just means this is a top-level task.
if ( (*todo)->relatedTo() )
{
Task* newParent = map.tqfind( (*todo)->relatedToUid() );
// Complete the loading but return a message
if ( !newParent )
err = i18n("Error loading \"%1\": could not find tqparent (uid=%2)")
.tqarg(task->name())
.tqarg((*todo)->relatedToUid());
if (!err) task->move( newParent);
}
}
kdDebug(5970) << "KarmStorage::load - loaded " << view->count()
<< " tasks from " << _icalfile << endl;
}
return err;
}
TQString KarmStorage::icalfile()
{
kdDebug(5970) << "Entering KarmStorage::icalfile" << endl;
return _icalfile;
}
TQString KarmStorage::buildTaskView(KCal::ResourceCalendar *rc, TaskView *view)
// makes *view contain the tasks out of *rc.
{
TQString err;
KCal::Todo::List todoList;
KCal::Todo::List::ConstIterator todo;
TQDict< Task > map;
vector<TQString> runningTasks;
vector<TQDateTime> startTimes;
// remember tasks that are running and their start times
for ( int i=0; i<view->count(); i++)
{
if ( view->item_at_index(i)->isRunning() )
{
runningTasks.push_back( view->item_at_index(i)->uid() );
startTimes.push_back( view->item_at_index(i)->lastStart() );
}
}
//view->stopAllTimers();
// delete old tasks
while (view->item_at_index(0)) view->item_at_index(0)->cut();
// 1. insert tasks form rc into taskview
// 1.1. Build dictionary to look up Task object from Todo uid. Each task is a
// TQListViewItem, and is initially added with the view as the tqparent.
todoList = rc->rawTodos();
for( todo = todoList.begin(); todo != todoList.end(); ++todo )
{
Task* task = new Task(*todo, view);
map.insert( (*todo)->uid(), task );
view->setRootIsDecorated(true);
task->setPixmapProgress();
}
// 1.1. Load each task under it's tqparent task.
for( todo = todoList.begin(); todo != todoList.end(); ++todo )
{
Task* task = map.tqfind( (*todo)->uid() );
// No relatedTo incident just means this is a top-level task.
if ( (*todo)->relatedTo() )
{
Task* newParent = map.tqfind( (*todo)->relatedToUid() );
// Complete the loading but return a message
if ( !newParent )
err = i18n("Error loading \"%1\": could not find tqparent (uid=%2)")
.tqarg(task->name())
.tqarg((*todo)->relatedToUid());
if (!err) task->move( newParent);
}
}
view->clearActiveTasks();
// restart tasks that have been running with their start times
for ( int i=0; i<view->count(); i++)
{
for ( unsigned int n=0; n<runningTasks.size(); n++)
{
if ( runningTasks[n] == view->item_at_index(i)->uid() )
{
view->startTimerFor( view->item_at_index(i), startTimes[n] );
}
}
}
view->refresh();
return err;
}
void KarmStorage::closeStorage(TaskView* view)
{
if ( _calendar )
{
_calendar->close();
delete _calendar;
_calendar = 0;
view->clear();
}
}
TQString KarmStorage::save(TaskView* taskview)
{
kdDebug(5970) << "entering KarmStorage::save" << endl;
TQString err=TQString();
TQPtrStack< KCal::Todo > parents;
for (Task* task=taskview->first_child(); task; task = task->nextSibling())
{
err=writeTaskAsTodo(task, 1, parents );
}
if ( !saveCalendar() )
{
err="Could not save";
}
if ( err.isEmpty() )
{
kdDebug(5970)
<< "KarmStorage::save : wrote "
<< taskview->count() << " tasks to " << _icalfile << endl;
}
else
{
kdWarning(5970) << "KarmStorage::save : " << err << endl;
}
return err;
}
TQString KarmStorage::writeTaskAsTodo(Task* task, const int level,
TQPtrStack< KCal::Todo >& parents )
{
TQString err;
KCal::Todo* todo;
todo = _calendar->todo(task->uid());
if ( !todo )
{
kdDebug(5970) << "Could not get todo from calendar" << endl;
return "Could not get todo from calendar";
}
task->asTodo(todo);
if ( !parents.isEmpty() ) todo->setRelatedTo( parents.top() );
parents.push( todo );
for ( Task* nextTask = task->firstChild(); nextTask;
nextTask = nextTask->nextSibling() )
{
err = writeTaskAsTodo(nextTask, level+1, parents );
}
parents.pop();
return err;
}
bool KarmStorage::isEmpty()
{
KCal::Todo::List todoList;
todoList = _calendar->rawTodos();
return todoList.empty();
}
bool KarmStorage::isNewStorage(const Preferences* preferences) const
{
if ( !_icalfile.isNull() ) return preferences->iCalFile() != _icalfile;
else return false;
}
//----------------------------------------------------------------------------
// Routines that handle legacy flat file format.
// These only stored total and session times.
//
TQString KarmStorage::loadFromFlatFile(TaskView* taskview,
const TQString& filename)
{
TQString err;
kdDebug(5970)
<< "KarmStorage::loadFromFlatFile: " << filename << endl;
TQFile f(filename);
if( !f.exists() )
err = i18n("File \"%1\" not found.").tqarg(filename);
if (!err)
{
if( !f.open( IO_ReadOnly ) )
err = i18n("Could not open \"%1\".").tqarg(filename);
}
if (!err)
{
TQString line;
TQPtrStack<Task> stack;
Task *task;
TQTextStream stream(&f);
while( !stream.atEnd() ) {
// lukas: this breaks for non-latin1 chars!!!
// if ( file.readLine( line, T_LINESIZE ) == 0 )
// break;
line = stream.readLine();
kdDebug(5970) << "DEBUG: line: " << line << "\n";
if (line.isNull())
break;
long minutes;
int level;
TQString name;
DesktopList desktopList;
if (!parseLine(line, &minutes, &name, &level, &desktopList))
continue;
unsigned int stackLevel = stack.count();
for (unsigned int i = level; i<=stackLevel ; i++) {
stack.pop();
}
if (level == 1) {
kdDebug(5970) << "KarmStorage::loadFromFlatFile - toplevel task: "
<< name << " min: " << minutes << "\n";
task = new Task(name, minutes, 0, desktopList, taskview);
task->setUid(addTask(task, 0));
}
else {
Task *tqparent = stack.top();
kdDebug(5970) << "KarmStorage::loadFromFlatFile - task: " << name
<< " min: " << minutes << " tqparent" << tqparent->name() << "\n";
task = new Task(name, minutes, 0, desktopList, tqparent);
task->setUid(addTask(task, tqparent));
// Legacy File Format (!):
tqparent->changeTimes(0, -minutes);
taskview->setRootIsDecorated(true);
tqparent->setOpen(true);
}
if (!task->uid().isNull())
stack.push(task);
else
delete task;
}
f.close();
}
return err;
}
TQString KarmStorage::loadFromFlatFileCumulative(TaskView* taskview,
const TQString& filename)
{
TQString err = loadFromFlatFile(taskview, filename);
if (!err)
{
for (Task* task = taskview->first_child(); task;
task = task->nextSibling())
{
adjustFromLegacyFileFormat(task);
}
}
return err;
}
bool KarmStorage::parseLine(TQString line, long *time, TQString *name,
int *level, DesktopList* desktopList)
{
if (line.tqfind('#') == 0) {
// A comment line
return false;
}
int index = line.tqfind('\t');
if (index == -1) {
// This doesn't seem like a valid record
return false;
}
TQString levelStr = line.left(index);
TQString rest = line.remove(0,index+1);
index = rest.tqfind('\t');
if (index == -1) {
// This doesn't seem like a valid record
return false;
}
TQString timeStr = rest.left(index);
rest = rest.remove(0,index+1);
bool ok;
index = rest.tqfind('\t'); // check for optional desktops string
if (index >= 0) {
*name = rest.left(index);
TQString deskLine = rest.remove(0,index+1);
// now transform the ds string (e.g. "3", or "1,4,5") into
// an DesktopList
TQString ds;
int d;
int commaIdx = deskLine.tqfind(',');
while (commaIdx >= 0) {
ds = deskLine.left(commaIdx);
d = ds.toInt(&ok);
if (!ok)
return false;
desktopList->push_back(d);
deskLine.remove(0,commaIdx+1);
commaIdx = deskLine.tqfind(',');
}
d = deskLine.toInt(&ok);
if (!ok)
return false;
desktopList->push_back(d);
}
else {
*name = rest.remove(0,index+1);
}
*time = timeStr.toLong(&ok);
if (!ok) {
// the time field was not a number
return false;
}
*level = levelStr.toInt(&ok);
if (!ok) {
// the time field was not a number
return false;
}
return true;
}
void KarmStorage::adjustFromLegacyFileFormat(Task* task)
{
// unless the tqparent is the listView
if ( task->tqparent() )
task->tqparent()->changeTimes(-task->sessionTime(), -task->time());
// traverse depth first -
// as soon as we're in a leaf, we'll substract it's time from the tqparent
// then, while descending back we'll do the same for each node untill
// we reach the root
for ( Task* subtask = task->firstChild(); subtask;
subtask = subtask->nextSibling() )
adjustFromLegacyFileFormat(subtask);
}
//----------------------------------------------------------------------------
// Routines that handle Comma-Separated Values export file format.
//
TQString KarmStorage::exportcsvFile( TaskView *taskview,
const ReportCriteria &rc )
{
TQString delim = rc.delimiter;
TQString dquote = rc.quote;
TQString double_dquote = dquote + dquote;
bool to_quote = true;
TQString err;
Task* task;
int maxdepth=0;
kdDebug(5970)
<< "KarmStorage::exportcsvFile: " << rc.url << endl;
TQString title = i18n("Export Progress");
KProgressDialog dialog( taskview, 0, title );
dialog.setAutoClose( true );
dialog.setAllowCancel( true );
dialog.progressBar()->setTotalSteps( 2 * taskview->count() );
// The default dialog was not displaying all the text in the title bar.
int width = taskview->fontMetrics().width(title) * 3;
TQSize dialogsize;
dialogsize.setWidth(width);
dialog.setInitialSize( dialogsize, true );
if ( taskview->count() > 1 ) dialog.show();
TQString retval;
// Find max task depth
int tasknr = 0;
while ( tasknr < taskview->count() && !dialog.wasCancelled() )
{
dialog.progressBar()->advance( 1 );
if ( tasknr % 15 == 0 ) kapp->processEvents(); // repainting is slow
if ( taskview->item_at_index(tasknr)->depth() > maxdepth )
maxdepth = taskview->item_at_index(tasknr)->depth();
tasknr++;
}
// Export to file
tasknr = 0;
while ( tasknr < taskview->count() && !dialog.wasCancelled() )
{
task = taskview->item_at_index( tasknr );
dialog.progressBar()->advance( 1 );
if ( tasknr % 15 == 0 ) kapp->processEvents();
// indent the task in the csv-file:
for ( int i=0; i < task->depth(); ++i ) retval += delim;
/*
// CSV compliance
// Surround the field with quotes if the field tqcontains
// a comma (delim) or a double quote
if (task->name().tqcontains(delim) || task->name().tqcontains(dquote))
to_quote = true;
else
to_quote = false;
*/
to_quote = true;
if (to_quote)
retval += dquote;
// Double quotes replaced by a pair of consecutive double quotes
retval += task->name().tqreplace( dquote, double_dquote );
if (to_quote)
retval += dquote;
// maybe other tasks are more indented, so to align the columns:
for ( int i = 0; i < maxdepth - task->depth(); ++i ) retval += delim;
retval += delim + formatTime( task->sessionTime(),
rc.decimalMinutes )
+ delim + formatTime( task->time(),
rc.decimalMinutes )
+ delim + formatTime( task->totalSessionTime(),
rc.decimalMinutes )
+ delim + formatTime( task->totalTime(),
rc.decimalMinutes )
+ "\n";
tasknr++;
}
// save, either locally or remote
if ((rc.url.isLocalFile()) || (!rc.url.url().tqcontains("/")))
{
TQString filename=rc.url.path();
if (filename.isEmpty()) filename=rc.url.url();
TQFile f( filename );
if( !f.open( IO_WriteOnly ) ) {
err = i18n( "Could not open \"%1\"." ).tqarg( filename );
}
if (!err)
{
TQTextStream stream(&f);
// Export to file
stream << retval;
f.close();
}
}
else // use remote file
{
KTempFile tmpFile;
if ( tmpFile.status() != 0 ) err = TQString::tqfromLatin1( "Unable to get temporary file" );
else
{
TQTextStream *stream=tmpFile.textStream();
*stream << retval;
tmpFile.close();
if (!KIO::NetAccess::upload( tmpFile.name(), rc.url, 0 )) err=TQString::tqfromLatin1("Could not upload");
}
}
return err;
}
//----------------------------------------------------------------------------
// Routines that handle logging KArm history
//
//
// public routines:
//
TQString KarmStorage::addTask(const Task* task, const Task* tqparent)
{
KCal::Todo* todo;
TQString uid;
todo = new KCal::Todo();
if ( _calendar->addTodo( todo ) )
{
task->asTodo( todo );
if (tqparent)
todo->setRelatedTo(_calendar->todo(tqparent->uid()));
uid = todo->uid();
}
else
{
// Most likely a lock could not be pulled, although there are other
// possiblities (like a really confused resource manager).
uid = "";
}
return uid;
}
bool KarmStorage::removeTask(Task* task)
{
// delete history
KCal::Event::List eventList = _calendar->rawEvents();
for(KCal::Event::List::iterator i = eventList.begin();
i != eventList.end();
++i)
{
//kdDebug(5970) << "KarmStorage::removeTask: "
// << (*i)->uid() << " - relatedToUid() "
// << (*i)->relatedToUid()
// << ", relatedTo() = " << (*i)->relatedTo() <<endl;
if ( (*i)->relatedToUid() == task->uid()
|| ( (*i)->relatedTo()
&& (*i)->relatedTo()->uid() == task->uid()))
{
_calendar->deleteEvent(*i);
}
}
// delete todo
KCal::Todo *todo = _calendar->todo(task->uid());
_calendar->deleteTodo(todo);
// Save entire file
saveCalendar();
return true;
}
void KarmStorage::addComment(const Task* task, const TQString& comment)
{
KCal::Todo* todo;
todo = _calendar->todo(task->uid());
// Do this to avoid compiler warnings about comment not being used. once we
// transition to using the addComment method, we need this second param.
TQString s = comment;
// TODO: Use libkcal comments
// todo->addComment(comment);
// temporary
todo->setDescription(task->comment());
saveCalendar();
}
long KarmStorage::printTaskHistory (
const Task *task,
const TQMap<TQString,long> &taskdaytotals,
TQMap<TQString,long> &daytotals,
const TQDate &from,
const TQDate &to,
const int level,
vector <TQString> &matrix,
const ReportCriteria &rc)
// to>=from is precondition
{
long ownline=linenr++; // the how many-th instance of this function is this
long colrectot=0; // colum where to write the task's total recursive time
vector <TQString> cell; // each line of the matrix is stored in an array of cells, one containing the recursive total
long add; // total recursive time of all subtasks
TQString delim = rc.delimiter;
TQString dquote = rc.quote;
TQString double_dquote = dquote + dquote;
bool to_quote = true;
const TQString cr = TQString::tqfromLatin1("\n");
TQString buf;
TQString daytaskkey, daykey;
TQDate day;
long sum;
if ( !task ) return 0;
day = from;
sum = 0;
while (day <= to)
{
// write the time in seconds for the given task for the given day to s
daykey = day.toString(TQString::tqfromLatin1("yyyyMMdd"));
daytaskkey = TQString::tqfromLatin1("%1_%2")
.tqarg(daykey)
.tqarg(task->uid());
if (taskdaytotals.tqcontains(daytaskkey))
{
cell.push_back(TQString::tqfromLatin1("%1")
.tqarg(formatTime(taskdaytotals[daytaskkey]/60, rc.decimalMinutes)));
sum += taskdaytotals[daytaskkey]; // in seconds
if (daytotals.tqcontains(daykey))
daytotals.tqreplace(daykey, daytotals[daykey]+taskdaytotals[daytaskkey]);
else
daytotals.insert(daykey, taskdaytotals[daytaskkey]);
}
cell.push_back(delim);
day = day.addDays(1);
}
// Total for task
cell.push_back(TQString::tqfromLatin1("%1").tqarg(formatTime(sum/60, rc.decimalMinutes)));
// room for the recursive total time (that cannot be calculated now)
cell.push_back(delim);
colrectot = cell.size();
cell.push_back("???");
cell.push_back(delim);
// Task name
for ( int i = level + 1; i > 0; i-- ) cell.push_back(delim);
/*
// CSV compliance
// Surround the field with quotes if the field tqcontains
// a comma (delim) or a double quote
to_quote = task->name().tqcontains(delim) || task->name().tqcontains(dquote);
*/
to_quote = true;
if ( to_quote) cell.push_back(dquote);
// Double quotes replaced by a pair of consecutive double quotes
cell.push_back(task->name().tqreplace( dquote, double_dquote ));
if ( to_quote) cell.push_back(dquote);
cell.push_back(cr);
add=0;
for (Task* subTask = task->firstChild();
subTask;
subTask = subTask->nextSibling())
{
add += printTaskHistory( subTask, taskdaytotals, daytotals, from, to , level+1, matrix,
rc );
}
cell[colrectot]=(TQString::tqfromLatin1("%1").tqarg(formatTime((add+sum)/60, rc.decimalMinutes )));
for (unsigned int i=0; i < cell.size(); i++) matrix[ownline]+=cell[i];
return add+sum;
}
TQString KarmStorage::report( TaskView *taskview, const ReportCriteria &rc )
{
TQString err;
if ( rc.reportType == ReportCriteria::CSVHistoryExport )
err = exportcsvHistory( taskview, rc.from, rc.to, rc );
else if ( rc.reportType == ReportCriteria::CSVTotalsExport )
err = exportcsvFile( taskview, rc );
else {
// hmmmm ... assert(0)?
}
return err;
}
// export history report as csv, all tasks X all dates in one block
TQString KarmStorage::exportcsvHistory ( TaskView *taskview,
const TQDate &from,
const TQDate &to,
const ReportCriteria &rc)
{
TQString delim = rc.delimiter;
const TQString cr = TQString::tqfromLatin1("\n");
TQString err;
// below taken from timekard.cpp
TQString retval;
TQString taskhdr, totalhdr;
TQString line, buf;
long sum;
TQValueList<HistoryEvent> events;
TQValueList<HistoryEvent>::iterator event;
TQMap<TQString, long> taskdaytotals;
TQMap<TQString, long> daytotals;
TQString daytaskkey, daykey;
TQDate day;
TQDate dayheading;
// parameter-plausi
if ( from > to )
{
err = TQString::tqfromLatin1 (
"'to' has to be a date later than or equal to 'from'.");
}
// header
retval += i18n("Task History\n");
retval += i18n("From %1 to %2")
.tqarg(KGlobal::locale()->formatDate(from))
.tqarg(KGlobal::locale()->formatDate(to));
retval += cr;
retval += i18n("Printed on: %1")
.tqarg(KGlobal::locale()->formatDateTime(TQDateTime::tqcurrentDateTime()));
retval += cr;
day=from;
events = taskview->getHistory(from, to);
taskdaytotals.clear();
daytotals.clear();
// Build lookup dictionary used to output data in table cells. keys are
// in this format: YYYYMMDD_NNNNNN, where Y = year, M = month, d = day and
// NNNNN = the VTODO uid. The value is the total seconds logged against
// that task on that day. Note the UID is the todo id, not the event id,
// so times are accumulated for each task.
for (event = events.begin(); event != events.end(); ++event)
{
daykey = (*event).start().date().toString(TQString::tqfromLatin1("yyyyMMdd"));
daytaskkey = TQString(TQString::tqfromLatin1("%1_%2"))
.tqarg(daykey)
.tqarg((*event).todoUid());
if (taskdaytotals.tqcontains(daytaskkey))
taskdaytotals.tqreplace(daytaskkey,
taskdaytotals[daytaskkey] + (*event).duration());
else
taskdaytotals.insert(daytaskkey, (*event).duration());
}
// day headings
dayheading = from;
while ( dayheading <= to )
{
// Use ISO 8601 format for date.
retval += dayheading.toString(TQString::tqfromLatin1("yyyy-MM-dd"));
retval += delim;
dayheading=dayheading.addDays(1);
}
retval += i18n("Sum") + delim + i18n("Total Sum") + delim + i18n("Task Hierarchy");
retval += cr;
retval += line;
// the tasks
vector <TQString> matrix;
linenr=0;
for (int i=0; i<=taskview->count()+1; i++) matrix.push_back("");
if (events.empty())
{
retval += i18n(" No hours logged.");
}
else
{
if ( rc.allTasks )
{
for ( Task* task= taskview->item_at_index(0);
task; task= task->nextSibling() )
{
printTaskHistory( task, taskdaytotals, daytotals, from, to, 0,
matrix, rc );
}
}
else
{
printTaskHistory( taskview->current_item(), taskdaytotals, daytotals,
from, to, 0, matrix, rc );
}
for (unsigned int i=0; i<matrix.size(); i++) retval+=matrix[i];
retval += line;
// totals
sum = 0;
day = from;
while (day<=to)
{
daykey = day.toString(TQString::tqfromLatin1("yyyyMMdd"));
if (daytotals.tqcontains(daykey))
{
retval += TQString::tqfromLatin1("%1")
.tqarg(formatTime(daytotals[daykey]/60, rc.decimalMinutes));
sum += daytotals[daykey]; // in seconds
}
retval += delim;
day = day.addDays(1);
}
retval += TQString::tqfromLatin1("%1%2%3%4")
.tqarg( formatTime( sum/60, rc.decimalMinutes ) )
.tqarg( delim ).tqarg( delim )
.tqarg( i18n( "Total" ) );
}
// above taken from timekard.cpp
// save, either locally or remote
if ((rc.url.isLocalFile()) || (!rc.url.url().tqcontains("/")))
{
TQString filename=rc.url.path();
if (filename.isEmpty()) filename=rc.url.url();
TQFile f( filename );
if( !f.open( IO_WriteOnly ) ) {
err = i18n( "Could not open \"%1\"." ).tqarg( filename );
}
if (!err)
{
TQTextStream stream(&f);
// Export to file
stream << retval;
f.close();
}
}
else // use remote file
{
KTempFile tmpFile;
if ( tmpFile.status() != 0 )
{
err = TQString::tqfromLatin1( "Unable to get temporary file" );
}
else
{
TQTextStream *stream=tmpFile.textStream();
*stream << retval;
tmpFile.close();
if (!KIO::NetAccess::upload( tmpFile.name(), rc.url, 0 )) err=TQString::tqfromLatin1("Could not upload");
}
}
return err;
}
void KarmStorage::stopTimer(const Task* task, TQDateTime when)
{
kdDebug(5970) << "Entering KarmStorage::stopTimer" << endl;
long delta = task->startTime().secsTo(when);
changeTime(task, delta);
}
bool KarmStorage::bookTime(const Task* task,
const TQDateTime& startDateTime,
const long durationInSeconds)
{
// Ignores preferences setting re: logging history.
KCal::Event* e;
TQDateTime end;
e = baseEvent( task );
e->setDtStart( startDateTime );
e->setDtEnd( startDateTime.addSecs( durationInSeconds ) );
// Use a custom property to keep a record of negative durations
e->setCustomProperty( kapp->instanceName(),
TQCString("duration"),
TQString::number(durationInSeconds));
return _calendar->addEvent(e);
}
void KarmStorage::changeTime(const Task* task, const long deltaSeconds)
{
kdDebug(5970) << "Entering KarmStorage::changeTime ( " << task->name() << "," << deltaSeconds << " )" << endl;
KCal::Event* e;
TQDateTime end;
// Don't write events (with timer start/stop duration) if user has turned
// this off in the settings dialog.
if ( ! task->taskView()->preferences()->logging() ) return;
e = baseEvent(task);
// Don't use duration, as ICalFormatImpl::writeIncidence never writes a
// duration, even though it looks like it's used in event.cpp.
end = task->startTime();
if ( deltaSeconds > 0 ) end = task->startTime().addSecs(deltaSeconds);
e->setDtEnd(end);
// Use a custom property to keep a record of negative durations
e->setCustomProperty( kapp->instanceName(),
TQCString("duration"),
TQString::number(deltaSeconds));
_calendar->addEvent(e);
// This saves the entire iCal file each time, which isn't efficient but
// ensures no data loss. A faster implementation would be to append events
// to a file, and then when KArm closes, append the data in this file to the
// iCal file.
//
// Meanwhile, we simply use a timer to delay the full-saving until the GUI
// has updated, for better user feedback. Feel free to get rid of this
// if/when implementing the faster saving (DF).
task->taskView()->scheduleSave();
}
KCal::Event* KarmStorage::baseEvent(const Task * task)
{
KCal::Event* e;
TQStringList categories;
e = new KCal::Event;
e->setSummary(task->name());
// Can't use setRelatedToUid()--no error, but no RelatedTo written to disk
e->setRelatedTo(_calendar->todo(task->uid()));
// Debugging: some events where not getting a related-to field written.
assert(e->relatedTo()->uid() == task->uid());
// Have to turn this off to get datetimes in date fields.
e->setFloats(false);
e->setDtStart(task->startTime());
// So someone can filter this mess out of their calendar display
categories.append(i18n("KArm"));
e->setCategories(categories);
return e;
}
HistoryEvent::HistoryEvent(TQString uid, TQString name, long duration,
TQDateTime start, TQDateTime stop, TQString todoUid)
{
_uid = uid;
_name = name;
_duration = duration;
_start = start;
_stop = stop;
_todoUid = todoUid;
}
TQValueList<HistoryEvent> KarmStorage::getHistory(const TQDate& from,
const TQDate& to)
{
TQValueList<HistoryEvent> retval;
TQStringList processed;
KCal::Event::List events;
KCal::Event::List::iterator event;
TQString duration;
for(TQDate d = from; d <= to; d = d.addDays(1))
{
events = _calendar->rawEventsForDate( d );
for (event = events.begin(); event != events.end(); ++event)
{
// KArm events have the custom property X-KDE-Karm-duration
if (! processed.tqcontains( (*event)->uid()))
{
// If an event spans multiple days, CalendarLocal::rawEventsForDate
// will return the same event on both days. To avoid double-counting
// such events, we (arbitrarily) attribute the hours from both days on
// the first day. This mis-reports the actual time spent, but it is
// an easy fix for a (hopefully) rare situation.
processed.append( (*event)->uid());
duration = (*event)->customProperty(kapp->instanceName(),
TQCString("duration"));
if ( ! duration.isNull() )
{
if ( (*event)->relatedTo()
&& ! (*event)->relatedTo()->uid().isEmpty() )
{
retval.append(HistoryEvent(
(*event)->uid(),
(*event)->summary(),
duration.toLong(),
(*event)->dtStart(),
(*event)->dtEnd(),
(*event)->relatedTo()->uid()
));
}
else
// Something is screwy with the ics file, as this KArm history event
// does not have a todo related to it. Could have been deleted
// manually? We'll continue with report on with report ...
kdDebug(5970) << "KarmStorage::getHistory(): "
<< "The event " << (*event)->uid()
<< " is not related to a todo. Dropped." << endl;
}
}
}
}
return retval;
}
bool KarmStorage::remoteResource( const TQString& file ) const
{
TQString f = file.lower();
bool rval = f.startsWith( "http://" ) || f.startsWith( "ftp://" );
kdDebug(5970) << "KarmStorage::remoteResource( " << file << " ) returns " << rval << endl;
return rval;
}
bool KarmStorage::saveCalendar()
{
kdDebug(5970) << "KarmStorage::saveCalendar" << endl;
#if 0
Event::List evl=_calendar->rawEvents();
kdDebug(5970) << "summary - dtStart - dtEnd" << endl;
for (unsigned int i=0; i<evl.count(); i++)
{
kdDebug() << evl[i]->summary() << evl[i]->dtStart() << evl[i]->dtEnd() << endl;
}
#endif
KABC::Lock *lock = _calendar->lock();
if ( !lock || !lock->lock() )
return false;
if ( _calendar && _calendar->save() ) {
lock->unlock();
return true;
}
lock->unlock();
return false;
}
|