summaryrefslogtreecommitdiff
path: root/src/backends/tdepim/TDEPIMNotesSource.cpp
blob: 265a3ce7d8b42ee4b6f974f0419b7e6bbf4846ed (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
/*
 * Copyright (C) 2016 Emanoil Kotsev emanoil.kotsev@fincom.at
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) version 3.
 *
 * 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301  USA
 *
 *
 * $Id: TDEPIMNotesSource.cpp,v 1.7 2016/09/12 19:57:27 emanoil Exp $
 *
 */

// #include <memory>
// using namespace std;

#include "config.h"

#ifdef ENABLE_TDEPIMNOTES

#include <syncevo/Exception.h>
#include <syncevo/SmartPtr.h>
#include <syncevo/Logging.h>

#include <tdeapplication.h>
#include <kmdcodec.h>
#include <dcopclient.h>

#include "TDEPIMNotesSource.h"

#include <syncevo/declarations.h>
SE_BEGIN_CXX

typedef TQString TDENoteID_t;

TDEPIMNotesSource::TDEPIMNotesSource( const SyncSourceParams &params ) : 
	TrackingSyncSource(params,1)
{
	//connect to dcop
	DCOPClient *kn_dcop = TDEApplication::kApplication()->dcopClient();
	if (!kn_dcop)
		Exception::throwError(SE_HERE, "internal init error, unable to make new dcop instance for tdenotes");

	appId = kn_dcop->registerAs("knotes-sync");

	//check knotes running
	QCStringList apps = kn_dcop->registeredApplications();
	if (!apps.contains("knotes")) {
		//start knotes if not running
		knotesWasRunning = false;
		system("knotes");
		system("dcop knotes KNotesIface hideAllNotes");
		SE_LOG_DEBUG(getDisplayName(), "knotes not running started OK");
	} else {
		knotesWasRunning = true;
		SE_LOG_DEBUG(getDisplayName(), "knotes was running OK");
	}

	kn_iface = new KNotesIface_stub("knotes", "KNotesIface");
	if ( ! kn_iface )
		Exception::throwError(SE_HERE, "internal error, KnotesIface");
/*	SyncSourceLogging::init(InitList<std::string>("SUMMARY") + "LOCATION",
				" ",
				m_operations);
*/
}

TDEPIMNotesSource::~TDEPIMNotesSource() {
	if ( ! knotesWasRunning )
		system("dcop knotes MainApplication-Interface quit");
	delete kn_iface;
	kn_iface = NULL;
	SE_LOG_DEBUG(getDisplayName(), "kNotes exit OK");
}

TQString TDEPIMNotesSource::stripHtml(TQString input)
{
	TQString output = NULL;
	unsigned int i = 0;
	int inbraces = 0;
	for (i = 0; i < input.length(); i++) {
		TQCharRef cur = input[i];
		if (cur == '<')
			inbraces = 1;
		if (cur == '>') {
			inbraces = 0;
			continue;
		}
		if (!inbraces)
			output += input[i];
	}
	return output.stripWhiteSpace();
}

TDEPIMNotesSource::Databases TDEPIMNotesSource::getDatabases()
{

	Databases result;

	const std::string name("tdenotes");
	const std::string path("tdepimnotes");

	result.push_back (
		Database ( 
			name,		// the name of the resource
			path,		// the path - (we use the resource uid)
			true,		// default or not
			false		// read only or not
		)
	);

	SE_LOG_DEBUG(getDisplayName(), "tdenotes getting database %s path: %s", name.c_str(), path.c_str());
	return result;
}

void TDEPIMNotesSource::open()
{
	std::string id = getDatabaseID();
	SE_LOG_DEBUG(getDisplayName(), "Resource id: %s opened OK", id.c_str() );
}

bool TDEPIMNotesSource::isEmpty()
{

	TQMap <TDENoteID_t,TQString> fNotes = kn_iface->notes();
	if (kn_iface->status() != DCOPStub::CallSucceeded)
		Exception::throwError(SE_HERE, "internal error, DCOP call failed");

	TQMap<TDENoteID_t,TQString>::ConstIterator i;
	for (i = fNotes.begin(); i != fNotes.end(); i++) {
		if (i.key().length() > 0)
			return false;
	}
	return true;
}

void TDEPIMNotesSource::close()
{
	const std::string id = getDatabaseID();
	SE_LOG_DEBUG(getDisplayName(), "Resource id: %s closed OK", id.c_str() );
}

void TDEPIMNotesSource::listAllItems(SyncSourceRevisions::RevisionMap_t &revisions)
{

	KMD5 hash_value;
	TQMap <TDENoteID_t,TQString> fNotes = kn_iface->notes();
	if (kn_iface->status() != DCOPStub::CallSucceeded)
		Exception::throwError(SE_HERE, "internal error, DCOP call failed");

	TQMap<TDENoteID_t,TQString>::ConstIterator i;
	for (i = fNotes.begin(); i != fNotes.end(); i++) {

		TQString data = i.data() + '\n' + stripHtml(kn_iface->text(i.key()));
		hash_value.update(data.utf8(),data.utf8().length());

		std::string uid_str(i.key().utf8(),i.key().utf8().length());
		revisions[uid_str] = hash_value.base64Digest().data();
		hash_value.reset();
/* DEBUG
		SE_LOG_DEBUG(getDisplayName(), "KNotes UID: %s", static_cast<const char*>(i.key().utf8()) );
		SE_LOG_DEBUG(getDisplayName(), "KNotes DATA: %s", static_cast<const char*>(data.utf8()));
*/
	}
}

TrackingSyncSource::InsertItemResult TDEPIMNotesSource::insertItem(const std::string &luid, const std::string &item, bool raw)
{

		InsertItemResultState state = ITEM_OKAY;
		TrackingSyncSource::InsertItemResult result;
		KMD5 hash_value;

		TQString uid  = TQString::fromUtf8(luid.data(), luid.size());
		TQString data = TQString::fromUtf8(item.data(), item.size());

		// store the hashed value of data to be able to find our new note later and get the id
		hash_value.update(data.utf8(),data.utf8().length());
		TQCString rev = hash_value.base64Digest();
		hash_value.reset();

		TQString summary = data.section('\n', 0, 0);  // first line is our title == summary
		TQString body = data.section('\n', 1);  // rest
/* DEBUG
		SE_LOG_DEBUG(getDisplayName(), "KNotes SUM : %s", static_cast<const char*>(summary.utf8()) );
		SE_LOG_DEBUG(getDisplayName(), "KNotes BODY: %s", static_cast<const char*>(body.utf8()) );
 */
		TQString newuid;

		TQString d = kn_iface->text( uid );
		if ( d.length() > 0 ) { // we have this note
				kn_iface->setName( uid, summary );
				kn_iface->setText( uid, body );
				newuid = uid;
		}
		else {
				newuid = kn_iface->newNote( summary, body );
				if (kn_iface->status() != DCOPStub::CallSucceeded)
					Exception::throwError(SE_HERE, "internal error, DCOP call failed");
				if ( ! newuid.length() > 0 )
						Exception::throwError(SE_HERE, "internal error, add note failed");
		}

		std::string ret_uid(newuid.utf8(), newuid.utf8().length());
		return InsertItemResult(ret_uid, rev.data(), state);

}

void TDEPIMNotesSource::readItem(const std::string &luid, std::string &item, bool raw)
{
	TQString uid = TQString::fromUtf8(luid.data(),luid.size());
	TQString data = kn_iface->name( uid ) + '\n' + stripHtml(kn_iface->text( uid ));

	std::string data_str( data.utf8(), data.utf8().length() );
	item.assign( data_str.c_str() );
}

void TDEPIMNotesSource::removeItem(const std::string &luid)
{
	TQString uid = TQString::fromUtf8(luid.data(),luid.size());
	TQString data = kn_iface->text( uid );
	if ( data.length() > 0 ) {
		kn_iface->killNote( uid );
		if (kn_iface->status() != DCOPStub::CallSucceeded)
			Exception::throwError(SE_HERE, "internal error, DCOP call failed");
	}
	else
		SE_LOG_DEBUG(getDisplayName(), "Item not found: id=%s", luid.c_str() );
}

std::string TDEPIMNotesSource::getDescription(const std::string &luid)
{
	TQString uid = TQString::fromUtf8(luid.data(),luid.size());
	TQString data = kn_iface->name( uid );
	if ( data.length() > 0 ) {
		std::string sum_str(data.utf8(),data.utf8().length());
		return sum_str;
	}
        SE_LOG_DEBUG(getDisplayName(), "Resource id(%s) not found", luid.c_str() );
	return "";
}

void TDEPIMNotesSource::getSynthesisInfo(SynthesisInfo &info, XMLConfigFragments &fragments)
{
	TrackingSyncSource::getSynthesisInfo(info, fragments);
	info.m_backendRule = "TDE";
	info.m_beforeWriteScript = "";
}

SE_END_CXX

#endif /* ENABLE_TDEPIMNOTES */

#ifdef ENABLE_MODULES
# include "TDEPIMNotesSourceRegister.cpp"
#endif