summaryrefslogtreecommitdiffabout
path: root/libkcal/phoneformat.cpp
Unidiff
Diffstat (limited to 'libkcal/phoneformat.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--libkcal/phoneformat.cpp117
1 files changed, 3 insertions, 114 deletions
diff --git a/libkcal/phoneformat.cpp b/libkcal/phoneformat.cpp
index b2a62b1..c39413e 100644
--- a/libkcal/phoneformat.cpp
+++ b/libkcal/phoneformat.cpp
@@ -1,239 +1,142 @@
1/* 1/*
2 This file is part of libkcal. 2 This file is part of libkcal.
3 3
4 Copyright (c) 2004 Lutz Rogowski <rogowski@kde.org> 4 Copyright (c) 2004 Lutz Rogowski <rogowski@kde.org>
5 5
6 This library is free software; you can redistribute it and/or 6 This library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public 7 modify it under the terms of the GNU Library General Public
8 License as published by the Free Software Foundation; either 8 License as published by the Free Software Foundation; either
9 version 2 of the License, or (at your option) any later version. 9 version 2 of the License, or (at your option) any later version.
10 10
11 This library is distributed in the hope that it will be useful, 11 This library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details. 14 Library General Public License for more details.
15 15
16 You should have received a copy of the GNU Library General Public License 16 You should have received a copy of the GNU Library General Public License
17 along with this library; see the file COPYING.LIB. If not, write to 17 along with this library; see the file COPYING.LIB. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. 19 Boston, MA 02111-1307, USA.
20*/ 20*/
21 21
22#include <qdatetime.h> 22#include <qdatetime.h>
23#include <qstring.h> 23#include <qstring.h>
24#include <qapplication.h> 24#include <qapplication.h>
25#include <qptrlist.h> 25#include <qptrlist.h>
26#include <qregexp.h> 26#include <qregexp.h>
27#include <qmessagebox.h> 27#include <qmessagebox.h>
28#include <qclipboard.h> 28#include <qclipboard.h>
29#include <qfile.h> 29#include <qfile.h>
30#include <qtextstream.h> 30#include <qtextstream.h>
31#include <qtextcodec.h> 31#include <qtextcodec.h>
32#include <qdir.h> 32#include <qdir.h>
33#include <qlabel.h> 33#include <qlabel.h>
34 34
35#include <kdebug.h> 35#include <kdebug.h>
36#include <klocale.h> 36#include <klocale.h>
37#include <kglobal.h> 37#include <kglobal.h>
38#include <kmessagebox.h> 38#include <kmessagebox.h>
39#include <phoneaccess.h>
39 40
40#include "calendar.h" 41#include "calendar.h"
41#include "alarm.h" 42#include "alarm.h"
42#include "recurrence.h" 43#include "recurrence.h"
43#include "calendarlocal.h" 44#include "calendarlocal.h"
44 45
45#include "phoneformat.h" 46#include "phoneformat.h"
46#include "syncdefines.h" 47#include "syncdefines.h"
47 48
48using namespace KCal; 49using namespace KCal;
49class PhoneParser : public QObject 50class PhoneParser : public QObject
50{ 51{
51public: 52public:
52 PhoneParser( ) { 53 PhoneParser( ) {
53 ; 54 ;
54 } 55 }
55 56
56 static QString dtToString( const QDateTime& dti, bool useTZ = false ) 57 static QString dtToString( const QDateTime& dti, bool useTZ = false )
57 { 58 {
58 QString datestr; 59 QString datestr;
59 QString timestr; 60 QString timestr;
60 int offset = KGlobal::locale()->localTimeOffset( dti ); 61 int offset = KGlobal::locale()->localTimeOffset( dti );
61 QDateTime dt; 62 QDateTime dt;
62 if (useTZ) 63 if (useTZ)
63 dt = dti.addSecs ( -(offset*60)); 64 dt = dti.addSecs ( -(offset*60));
64 else 65 else
65 dt = dti; 66 dt = dti;
66 if(dt.date().isValid()){ 67 if(dt.date().isValid()){
67 const QDate& date = dt.date(); 68 const QDate& date = dt.date();
68 datestr.sprintf("%04d%02d%02d", 69 datestr.sprintf("%04d%02d%02d",
69 date.year(), date.month(), date.day()); 70 date.year(), date.month(), date.day());
70 } 71 }
71 if(dt.time().isValid()){ 72 if(dt.time().isValid()){
72 const QTime& time = dt.time(); 73 const QTime& time = dt.time();
73 timestr.sprintf("T%02d%02d%02d", 74 timestr.sprintf("T%02d%02d%02d",
74 time.hour(), time.minute(), time.second()); 75 time.hour(), time.minute(), time.second());
75 } 76 }
76 return datestr + timestr; 77 return datestr + timestr;
77 } 78 }
78 79
79 80
80}; 81};
81 82
82 83
83 84
84PhoneFormat::PhoneFormat(QString profileName, QString device,QString connection, QString model ) 85PhoneFormat::PhoneFormat(QString profileName, QString device,QString connection, QString model )
85{ 86{
86 mProfileName = profileName; 87 mProfileName = profileName;
87 writeConfig( device, connection, model ); 88 PhoneAccess::writeConfig( device, connection, model );
88} 89}
89 90
90PhoneFormat::~PhoneFormat() 91PhoneFormat::~PhoneFormat()
91{ 92{
92} 93}
93void PhoneFormat::writeConfig( QString device, QString connection, QString model )
94{
95#ifdef _WIN32_
96 QString fileName = qApp->applicationDirPath () +"\\gammurc";
97#else
98 QString fileName = QDir::homeDirPath() +"/.gammurc";
99#endif
100 //qDebug("save %d ", load );
101 QString content;
102 bool write = false;
103 bool addPort = true, addConnection = true, addModel = true;
104 QFile file( fileName );
105 if ( QFile::exists( fileName) ) {
106 if (!file.open( IO_ReadOnly ) ) {
107 qDebug("Error: cannot open %s ", fileName.latin1() );
108 return;
109 }
110 QString line;
111 while ( file.readLine( line, 1024 ) > 0 ) {
112 //qDebug("*%s* ", line.latin1() );
113 if ( line.left(7 ) == "[gammu]" ) {
114 ;
115 } else
116 if ( line.left(4 ) == "port" ) {
117 if ( line == "port = " + device+"\n" ) {
118 content += line ;
119 addPort = false;
120 //qDebug("port found" );
121 }
122
123 } else if ( line.left(5 ) == "model" ) {
124 if ( line == "model = " + model +"\n") {
125 content += line ;
126 addModel = false;
127 //qDebug("model found" );
128 }
129
130 } else if ( line.left( 10 ) == "connection" ) {
131 if ( line == "connection = " + connection +"\n") {
132 addConnection = false;
133 content += line ;
134 //qDebug("con found" );
135 }
136
137 } else {
138 content += line ;
139 }
140 }
141 file.close();
142 } else {
143 if ( ! connection.isEmpty() ) {
144 addConnection = true;
145 }
146 if ( ! device.isEmpty() ) {
147 addPort = true;
148 94
149 }
150 if ( ! model.isEmpty() ) {
151 addModel = true;
152 }
153 }
154
155 if ( addConnection ) {
156 if ( ! write )
157 content += "[gammu]\n";
158 write = true;
159 content += "connection = ";
160 content += connection;
161 content += "\n";
162 }
163 if ( addPort ) {
164 if ( ! write )
165 content += "[gammu]\n";
166 write = true;
167 content += "port = ";
168 content += device;
169 content += "\n";
170
171 }
172 if ( addModel ) {
173 if ( ! write )
174 content += "[gammu]\n";
175 write = true;
176 content += "model = ";
177 content += model;
178 content += "\n";
179 }
180 if ( write ) {
181 if (!file.open( IO_WriteOnly ) ) {
182 qDebug("Error: cannot write file %s ", fileName.latin1() );
183 return;
184 }
185 qDebug("Writing file %s ", fileName.latin1() );
186 QTextStream ts( &file );
187 ts << content ;
188 file.close();
189 }
190
191}
192#if 0 95#if 0
193int PhoneFormat::initDevice(GSM_StateMachine *s) 96int PhoneFormat::initDevice(GSM_StateMachine *s)
194{ 97{
195 GSM_ReadConfig(NULL, &s->Config[0], 0); 98 GSM_ReadConfig(NULL, &s->Config[0], 0);
196 s->ConfigNum = 1; 99 s->ConfigNum = 1;
197 GSM_Config *cfg = &s->Config[0]; 100 GSM_Config *cfg = &s->Config[0];
198 if ( ! mConnection.isEmpty() ) { 101 if ( ! mConnection.isEmpty() ) {
199 cfg->Connection = strdup(mConnection.latin1()); 102 cfg->Connection = strdup(mConnection.latin1());
200 cfg->DefaultConnection = false; 103 cfg->DefaultConnection = false;
201 qDebug("Connection set %s ", cfg->Connection ); 104 qDebug("Connection set %s ", cfg->Connection );
202 105
203 } 106 }
204 if ( ! mDevice.isEmpty() ) { 107 if ( ! mDevice.isEmpty() ) {
205 cfg->Device = strdup(mDevice.latin1()); 108 cfg->Device = strdup(mDevice.latin1());
206 cfg->DefaultDevice = false; 109 cfg->DefaultDevice = false;
207 qDebug("Device set %s ", cfg->Device); 110 qDebug("Device set %s ", cfg->Device);
208 111
209 } 112 }
210 if ( ! mModel.isEmpty() ) { 113 if ( ! mModel.isEmpty() ) {
211 strcpy(cfg->Model,mModel.latin1() ); 114 strcpy(cfg->Model,mModel.latin1() );
212 cfg->DefaultModel = false; 115 cfg->DefaultModel = false;
213 qDebug("Model set %s ",cfg->Model ); 116 qDebug("Model set %s ",cfg->Model );
214 } 117 }
215 int error=GSM_InitConnection(s,3); 118 int error=GSM_InitConnection(s,3);
216 return error; 119 return error;
217} 120}
218#endif 121#endif
219ulong PhoneFormat::getCsumTodo( Todo* todo ) 122ulong PhoneFormat::getCsumTodo( Todo* todo )
220{ 123{
221 QStringList attList; 124 QStringList attList;
222 if ( todo->hasDueDate() ) 125 if ( todo->hasDueDate() )
223 attList << PhoneParser::dtToString ( todo->dtDue() ); 126 attList << PhoneParser::dtToString ( todo->dtDue() );
224 attList << todo->summary(); 127 attList << todo->summary();
225 QString completedString = "no"; 128 QString completedString = "no";
226 if ( todo->isCompleted() ) 129 if ( todo->isCompleted() )
227 completedString = "yes"; 130 completedString = "yes";
228 attList << completedString; 131 attList << completedString;
229 int prio = todo->priority(); 132 int prio = todo->priority();
230 if( prio == 2 ) prio = 1; 133 if( prio == 2 ) prio = 1;
231 if (prio == 4 ) prio = 5 ; 134 if (prio == 4 ) prio = 5 ;
232 attList << QString::number( prio ); 135 attList << QString::number( prio );
233 QString alarmString = "na"; 136 QString alarmString = "na";
234 Alarm *alarm; 137 Alarm *alarm;
235 if ( todo->alarms().count() > 0 ) { 138 if ( todo->alarms().count() > 0 ) {
236 alarm = todo->alarms().first(); 139 alarm = todo->alarms().first();
237 if ( alarm->enabled() ) { 140 if ( alarm->enabled() ) {
238 alarmString = QString::number(alarm->offset() ); 141 alarmString = QString::number(alarm->offset() );
239 } 142 }
@@ -545,111 +448,97 @@ void PhoneFormat::copyTodo( Todo* to, Todo* from )
545 } 448 }
546 } 449 }
547 to->setCategories( cat ); 450 to->setCategories( cat );
548 if ( from->isCompleted() ) { 451 if ( from->isCompleted() ) {
549 to->setCompleted( true ); 452 to->setCompleted( true );
550 if( from->completed().isValid() ) 453 if( from->completed().isValid() )
551 to->setCompleted( from->completed() ); 454 to->setCompleted( from->completed() );
552 } else { 455 } else {
553 // set percentcomplete only, if to->isCompleted() 456 // set percentcomplete only, if to->isCompleted()
554 if ( to->isCompleted() ) 457 if ( to->isCompleted() )
555 to->setPercentComplete(from->percentComplete()); 458 to->setPercentComplete(from->percentComplete());
556 } 459 }
557 if( to->priority() == 2 && from->priority() == 1 ) 460 if( to->priority() == 2 && from->priority() == 1 )
558 ; //skip 461 ; //skip
559 else if (to->priority() == 4 && from->priority() == 5 ) 462 else if (to->priority() == 4 && from->priority() == 5 )
560 ; 463 ;
561 else 464 else
562 to->setPriority(from->priority()); 465 to->setPriority(from->priority());
563 466
564} 467}
565#include <qcstring.h> 468#include <qcstring.h>
566 469
567void PhoneFormat::afterSave( Incidence* inc) 470void PhoneFormat::afterSave( Incidence* inc)
568{ 471{
569 uint csum; 472 uint csum;
570 inc->removeID( mProfileName ); 473 inc->removeID( mProfileName );
571 if ( inc->type() == "Event") 474 if ( inc->type() == "Event")
572 csum = PhoneFormat::getCsumEvent( (Event*) inc ); 475 csum = PhoneFormat::getCsumEvent( (Event*) inc );
573 else 476 else
574 csum = PhoneFormat::getCsumTodo( (Todo*) inc ); 477 csum = PhoneFormat::getCsumTodo( (Todo*) inc );
575 inc->setCsum( mProfileName, QString::number( csum )); 478 inc->setCsum( mProfileName, QString::number( csum ));
576 479
577 inc->setTempSyncStat( SYNC_TEMPSTATE_NEW_ID ); 480 inc->setTempSyncStat( SYNC_TEMPSTATE_NEW_ID );
578 481
579} 482}
580 483
581bool PhoneFormat::writeToPhone( Calendar * calendar) 484bool PhoneFormat::writeToPhone( Calendar * calendar)
582{ 485{
583#ifdef _WIN32_ 486#ifdef _WIN32_
584 QString fileName = locateLocal("tmp", "tempfile.vcs"); 487 QString fileName = locateLocal("tmp", "tempfile.vcs");
585#else 488#else
586 QString fileName = "/tmp/kdepimtemp.vcs"; 489 QString fileName = "/tmp/kdepimtemp.vcs";
587#endif 490#endif
588 491
589 VCalFormat vfsave; 492 VCalFormat vfsave;
590 vfsave.setLocalTime ( true ); 493 vfsave.setLocalTime ( true );
591 if ( ! vfsave.save( calendar, fileName ) ) 494 if ( ! vfsave.save( calendar, fileName ) )
592 return false; 495 return false;
593 // 4 call kammu 496 return PhoneAccess::writeToPhone( fileName );
594#ifdef DESKTOP_VERSION
595 QString command ="./kammu --restore " + fileName ;
596#else
597 QString command ="kammu --restore " + fileName ;
598#endif
599 int ret;
600 while ( (ret = system ( command.latin1())) != 0 ) {
601 qDebug("Error S::command returned %d. asking users", ret);
602 int retval = KMessageBox::warningContinueCancel(0,
603 i18n("Error accessing device!\nPlease turn on connection\nand retry!"),i18n("KO/Pi phone access"),i18n("Retry"),i18n("Cancel"));
604 if ( retval != KMessageBox::Continue )
605 return false;
606 }
607 return true;
608} 497}
609bool PhoneFormat::save( Calendar *calendar) 498bool PhoneFormat::save( Calendar *calendar)
610{ 499{
611 QLabel status ( i18n(" Opening device ..."), 0 ); 500 QLabel status ( i18n(" Opening device ..."), 0 );
612 int w = status.sizeHint().width()+20 ; 501 int w = status.sizeHint().width()+20 ;
613 if ( w < 200 ) w = 230; 502 if ( w < 200 ) w = 230;
614 int h = status.sizeHint().height()+20 ; 503 int h = status.sizeHint().height()+20 ;
615 int dw = QApplication::desktop()->width(); 504 int dw = QApplication::desktop()->width();
616 int dh = QApplication::desktop()->height(); 505 int dh = QApplication::desktop()->height();
617 status.setCaption(i18n("Writing to phone...") ); 506 status.setCaption(i18n("Writing to phone...") );
618 status.setGeometry( (dw-w)/2, (dh - h )/2 ,w,h ); 507 status.setGeometry( (dw-w)/2, (dh - h )/2 ,w,h );
619 status.show(); 508 status.show();
620 status.raise(); 509 status.raise();
621 qApp->processEvents(); 510 qApp->processEvents();
622 QString message; 511 QString message;
623 512
624 // 1 remove events which should be deleted 513 // 1 remove events which should be deleted
625 QPtrList<Event> er = calendar->rawEvents(); 514 QPtrList<Event> er = calendar->rawEvents();
626 Event* ev = er.first(); 515 Event* ev = er.first();
627 while ( ev ) { 516 while ( ev ) {
628 if ( ev->tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { 517 if ( ev->tempSyncStat() == SYNC_TEMPSTATE_DELETE ) {
629 calendar->deleteEvent( ev ); 518 calendar->deleteEvent( ev );
630 } else { 519 } else {
631 520
632 } 521 }
633 ev = er.next(); 522 ev = er.next();
634 } 523 }
635 // 2 remove todos which should be deleted 524 // 2 remove todos which should be deleted
636 QPtrList<Todo> tl = calendar->rawTodos(); 525 QPtrList<Todo> tl = calendar->rawTodos();
637 Todo* to = tl.first(); 526 Todo* to = tl.first();
638 while ( to ) { 527 while ( to ) {
639 if ( to->tempSyncStat() == SYNC_TEMPSTATE_DELETE ) { 528 if ( to->tempSyncStat() == SYNC_TEMPSTATE_DELETE ) {
640 calendar->deleteTodo( to ); 529 calendar->deleteTodo( to );
641 } else { 530 } else {
642 if ( to->isCompleted()) { 531 if ( to->isCompleted()) {
643 calendar->deleteTodo( to ); 532 calendar->deleteTodo( to );
644 } 533 }
645 } 534 }
646 to = tl.next(); 535 to = tl.next();
647 } 536 }
648 // 3 save file 537 // 3 save file
649 if ( !writeToPhone( calendar ) ) 538 if ( !writeToPhone( calendar ) )
650 return false; 539 return false;
651 540
652 // 5 reread data 541 // 5 reread data
653 message = i18n(" Rereading all data ... "); 542 message = i18n(" Rereading all data ... ");
654 status.setText ( message ); 543 status.setText ( message );
655 qApp->processEvents(); 544 qApp->processEvents();