summaryrefslogtreecommitdiffabout
path: root/plugindtmkabc/qtopiaaddressbookplugin.cpp
blob: 37dd4e2005439151e66132662ca2084e4cee21fe (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
#include <qwidget.h>
#include <qlayout.h>
#include <qlistbox.h>
#include <qstring.h>
#include <qfile.h>
#include <qtextstream.h>
#include <qtextcodec.h>
#include <stdlib.h>
#include "qtopiaaddressbookplugin.h"
#include "qtopiaaddressee.h"

QtopiaAddressBookPlugin::QtopiaAddressBookPlugin() : ref(0) {
  qDebug("DTMAddressBookPlugin::DTMAddressBookPlugin");
  QString command ="db2file address -r -c utf8 > /tmp/addressDTM.txt";
  system (  command.latin1() );
}

QtopiaAddressBookPlugin::~QtopiaAddressBookPlugin() {
}

KABC::Addressee::List QtopiaAddressBookPlugin::getAddressees() {
  
  qDebug("Start: DTMAddressBookPlugin::getAddressees");

  KABC::Addressee::List result;
  QString text;
  QString  fileName = "/tmp/addressDTM.txt"; 
  QFile file( fileName );
  if (!file.open( IO_ReadOnly ) ) {
      return result;
  } 
  QTextStream ts( &file );
  ts.setCodec( QTextCodec::codecForName("utf8") );
  text = ts.read();
  file.close();  

  QStringList templist;
  QString tempString;
  int start = 0;
  int len = text.length();
  int end = text.find ("\n",start)+1;
  bool ok = true;
  start = end;
  int lastStart1 = -1;
  int lastStart2 = -1;
  while ( start > 0 ) {
      //qDebug("while start %d ", start); 
      if ( lastStart1 == start )
          break;
      lastStart1 =start;
      templist.clear();
      ok = true;
      int iii = 0;
      while ( ok ) {
     
          // qDebug("while ok %d ", start);

          if ( lastStart2 == start )
              break;
          lastStart2 =start;
          tempString = getPart(  text, ok, start );
          //if ( ! tempString.isEmpty() )
          //qDebug("tempString %s %d",tempString.latin1(), iii);
          iii++;
          if ( start >= len || start == 0 ) {
              start = 0;
              ok = false;
          }
          if ( tempString.right(1) =="\n" )
              tempString = tempString.left( tempString.length()-1);
          //if ( ok ) 
          templist.append( tempString ); 
          //qDebug("%d ---%s---", templist.count(),tempString.latin1() );
      }
      result.append(QtopiaAddressee(templist)); 
      //qDebug("name %s ",templist[2].latin1() );
      //qDebug("name %s ",templist[4].latin1() );
      //qDebug("name %s ",templist[5].latin1() );
      //qDebug("name %s ",templist[40].latin1() );
  }
  
  
  qDebug("End: DTMAddressBookPlugin::getAddressees"); 
  
  return result;
}

QString QtopiaAddressBookPlugin::getPart( const QString& text , bool &ok, int &start )
{
  //qDebug("start %d ", start);
  
    QString retval ="";
    if ( text.at(start) == '"' ) {
        if ( text.mid( start,2) == "\"\"" && !( text.mid( start+2,1) == "\"")) {
            start = start +2; 
            if ( text.mid( start,1) == "," ) {
                start += 1;
            }
            retval = "";
            if ( text.mid( start,1) == "\n" ) {
                start += 1;
                ok = false; 
            }
            return retval;
        }
        int hk = start+1;
        hk = text.find ('"',hk);
        while ( text.at(hk+1) == '"' )
            hk = text.find ('"',hk+2);
        retval = text.mid( start+1, hk-start-1);
        start = hk+1;
        retval.replace( QRegExp("\"\""), "\""); 
        if ( text.mid( start,1) == "," ) {
            start += 1;
        }
        if ( text.mid( start,1) == "\n" ) {
            start += 1;
            ok = false; 
        }
        //qDebug("retval***%s*** ",retval.latin1() );
        return retval;

    } else {
        int nl = text.find ("\n",start);
        int kom = text.find (',',start);
        if ( kom < nl ) {
            // qDebug("kom < nl %d ", kom);
            retval = text.mid(start, kom-start);
            start = kom+1;
            return retval;
        } else {
            if ( nl == kom ) {
                // qDebug(" nl == kom ");
                start = 0;
                ok = false;
                return "0";
            }
            // qDebug(" nl < kom ", nl);
            retval = text.mid( start, nl-start);
            ok = false;
            start = nl+1;
            return retval;
        }
    }

}
QString QtopiaAddressBookPlugin::name() {
  return "DTM addressbook plugin";
}

QRESULT QtopiaAddressBookPlugin::queryInterface( const QUuid &uuid, QUnknownInterface **iface )
{
    *iface = 0;
    if ( uuid == IID_QUnknown )
      *iface = this;
    else if ( uuid == IID_KOAddressBookInterface )
      *iface = this;
    if ( *iface )
      (*iface)->addRef();
    return QS_OK;
}

Q_EXPORT_INTERFACE()
{
    Q_CREATE_INSTANCE( QtopiaAddressBookPlugin )
}