summaryrefslogtreecommitdiffstats
path: root/kresources/egroupware/todostatemapper.cpp
blob: ed87e9db679d7e494899c57d3be7c23c14418cf7 (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
/*
    This file is part of kdepim.

    Copyright (c) 2005 Tobias Koenig <tokoe@kde.org>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include <tqdatastream.h>
#include <tqfile.h>

#include <kdebug.h>
#include <kstandarddirs.h>

#include "todostatemapper.h"

TQDataStream& operator<<( TQDataStream &stream, const TodoStateMapper::TodoStateMapEntry &entry )
{
  stream << entry.uid << entry.localState << entry.remoteState;

  return stream;
}

TQDataStream& operator>>( TQDataStream &stream, TodoStateMapper::TodoStateMapEntry &entry )
{
  stream >> entry.uid >> entry.localState >> entry.remoteState;

  return stream;
}

TodoStateMapper::TodoStateMapper()
{
}

TodoStateMapper::~TodoStateMapper()
{
}

void TodoStateMapper::setPath( const TQString &path )
{
  mPath = path;
}

void TodoStateMapper::setIdentifier( const TQString &identifier )
{
  mIdentifier = identifier;
}

bool TodoStateMapper::load()
{
  TQFile file( filename() );
  if ( !file.open( IO_ReadOnly ) ) {
    kdError() << "Can't read uid map file '" << filename() << "'" << endl;
    return false;
  }

  clear();

  TQDataStream stream;
  stream.setVersion( 6 );
  stream.setDevice( &file );
  stream >> mTodoStateMap;

  file.close();

  return true;
}

bool TodoStateMapper::save()
{
  TQFile file( filename() );
  if ( !file.open( IO_WriteOnly ) ) {
    kdError() << "Can't write uid map file '" << filename() << "'" << endl;
    return false;
  }

  TQDataStream stream;
  stream.setVersion( 6 );
  stream.setDevice( &file );
  stream << mTodoStateMap;

  file.close();

  return true;
}

void TodoStateMapper::clear()
{
  mTodoStateMap.clear();
}

void TodoStateMapper::addTodoState( const TQString &uid, int localState, const TQString &remoteState )
{
  TodoStateMapEntry entry;
  entry.uid = uid;
  entry.localState = localState;
  entry.remoteState = remoteState;

  mTodoStateMap.insert( uid, entry );
}

TQString TodoStateMapper::remoteState( const TQString &uid, int localState )
{
  if ( mTodoStateMap.find( uid ) == mTodoStateMap.end() )
    kdError() << "TodoStateMapper: no entry for " << uid << " found" << endl;

  TodoStateMapEntry entry = mTodoStateMap[ uid ];
  if ( entry.localState == localState )
    return entry.remoteState;
  else
    return toRemote( localState );
}

void TodoStateMapper::remove( const TQString &uid )
{
  mTodoStateMap.remove( uid );
}

int TodoStateMapper::toLocal( const TQString &remoteState )
{
  if ( remoteState == "offer" )
    return 0;
  else if ( remoteState == "ongoing" )
    return 50;
  else if ( remoteState == "done" || remoteState == "billed" )
    return 100;
  else {
    TQString number( remoteState );
    number.replace( "%", "" );
    return number.toInt();
  }
}

TQString TodoStateMapper::toRemote( int localState )
{
  if ( localState == 0 )
    return "offer";
  else if ( localState == 50 )
    return "ongoing";
  else if ( localState == 100 )
    return "done";
  else
    return TQString( "%1%" ).tqarg( localState );
}

TQString TodoStateMapper::filename()
{
  TQString file = mPath;
  if ( !file.endsWith( "/" ) ) file += "/";
  file += mIdentifier;

  return locateLocal( "data", file );
}