summaryrefslogtreecommitdiff
path: root/portland/libxdg-vfs/libxdg-vfs-client/src/xdg_vfs_client.h
blob: 326ad2915c5d8e09cd626cfe0d09a18087e96198 (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
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
/*
#   xdg_vfs_client.h
#
#   Copyright 2006, Norbert Frese
#
#   LICENSE:
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included
#   in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
#   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
#   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.
#
#---------------------------------------------*/


#ifndef XDGVFS_CLIENT_H_
#define XDGVFS_CLIENT_H_

#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <xdg_vfs_common.h>

#define XDG_VFS_DEBUG_IPC 0

#define XDGVFS_CLIENT_BUFFERSIZE 8192

typedef size_t XdgVfsFileSize;


/* ====== incoming Items ======== */


/**
*  itemtypes
*/
typedef enum {
	XDGVFS_ITEMTYPE_FILEINFO = 1,
	XDGVFS_ITEMTYPE_SETATTRS_HEAD = 2,
	XDGVFS_ITEMTYPE_DIR_HEAD = 3,
	XDGVFS_ITEMTYPE_DIR_ENTRY = 4,
	XDGVFS_ITEMTYPE_PROGRESS = 5,
	XDGVFS_ITEMTYPE_GET_HEAD = 6,
	XDGVFS_ITEMTYPE_PUT_HEAD = 7,
	XDGVFS_ITEMTYPE_COPY_HEAD = 8,
	XDGVFS_ITEMTYPE_MOVE_HEAD = 9,
	XDGVFS_ITEMTYPE_MKDIR_HEAD = 10,
	XDGVFS_ITEMTYPE_RMDIR_HEAD = 11,
	XDGVFS_ITEMTYPE_RM_HEAD = 12,
	XDGVFS_ITEMTYPE_LS_HEAD = 13,
	XDGVFS_ITEMTYPE_OPENFILEDLG_RESPONSE = 14,
	XDGVFS_ITEMTYPE_SAVEFILEDLG_RESPONSE = 15,
	XDGVFS_ITEMTYPE_DATAIN_DONE = 16,
	XDGVFS_ITEMTYPE_DATAOUT_DONE = 17,
	XDGVFS_DATAIN = 18,
	XDGVFS_DATAOUT = 19,
	XDGVFS_ITEMTYPE_BACKENDINFO = 20,

	XDGVFS_ITEMTYPE_NONE = 0
} XdgVfsItemType;


typedef struct {
	XdgVfsItemType type;
	int refcount;
} XdgVfsItem;

typedef struct {
	XdgVfsItem itm;
	char * backend_id;
	char * system_uri;
	char * file_icon_theme;
} XdgVfsBackendInfo;


typedef struct {
	XdgVfsItem itm;
	XdgVfsFileSize bytecount;
} XdgVfsDataInDoneItem;

typedef struct {
	XdgVfsItem itm;
	XdgVfsFileSize bytecount;
} XdgVfsDataOutDoneItem;

typedef struct {
	XdgVfsItem itm;
	char * uri;
} XdgVfsSimpleHead;

/**
*  fileinfo filetypes
*/
typedef enum {
	XDGVFS_FILE_TYPE_UNKNOWN = 1,
	XDGVFS_FILE_TYPE_REGULAR = 2,
	XDGVFS_FILE_TYPE_DIRECTORY = 3,
	XDGVFS_FILE_TYPE_FIFO = 4,
	XDGVFS_FILE_TYPE_SOCKET = 5,
	XDGVFS_FILE_TYPE_CHARDEV = 6,
	XDGVFS_FILE_TYPE_BLOCKDEV = 7,
	XDGVFS_FILE_TYPE_SYMLINK = 8,
	XDGVFS_FILE_TYPE_VFSMOUNTPOINT = 9,
	XDGVFS_FILE_TYPE_UNREC = 0
} XdgVfsFileType;

/**
*  fileinfo item
*/
typedef struct {
	XdgVfsItem itm;
	char * uri;
	char * basename;
	char * mimetype;
	size_t size;	
	int permissions;
	char * user;
	char * group;
	int atime;
	int ctime;
	int mtime;
	char * linkdest;
	XdgVfsFileType filetype;
	char * iconname;
	char * mountpoint_id;
	int is_mounted;
} XdgVfsFileInfo;

typedef struct {
	XdgVfsItem itm;
	char * selected_localpath;
	char * selected_uri;
} XdgVfsOpenFileDlgResponse;

typedef struct {
	XdgVfsItem itm;
	char * selected_localpath;
	char * selected_uri;
} XdgVfsSaveFileDlgResponse;

typedef struct {
	XdgVfsItem itm;
	char * uri_src;
	char * uri_target;
} XdgVfsCopyHead;

typedef struct {
	XdgVfsItem itm;
	char * uri_src;
	char * uri_target;
} XdgVfsMoveHead;

typedef struct {
	XdgVfsItem itm;
	int percent;
	XdgVfsFileSize size;
	XdgVfsFileSize copied;
} XdgVfsProgress;


/* ====== flags and return values ======== */

/*
*  Command flags 
*/
typedef enum {
	XDGVFS_FLAGS_NONE = 0,
	XDGVFS_FLAGS_OVERWRITE = 2,
	XDGVFS_FLAGS_INSTANT_GET = 4,
	XDGVFS_FLAGS_INSTANT_PUT = 8,
	XDGVFS_FLAGS_QRY_FILEINFO = 16
} XdgVfsFlags;

/**
*  xdg-vfs return values: 
*  values less than 0 are client errors.
*  values greater than 0 are XDGVFS_ERRORCODE_* errors!
*/
#define XDGVFS_RESULT_CANNOT_LAUNCH_BACKEND -10
#define XDGVFS_RESULT_CALL_SENDDATA_AGAIN -9
#define XDGVFS_RESULT_GIVE_ME_DATA -8
#define XDGVFS_RESULT_PARAMETER_ERR -7
#define XDGVFS_RESULT_FATAL_ERR -6
#define XDGVFS_RESULT_UNESCAPING_DATA_ERR -5
#define XDGVFS_RESULT_BAD_STATE -4
#define XDGVFS_RESULT_IOERR -3
#define XDGVFS_RESULT_NOT_POSSIBLE -2
#define XDGVFS_RESULT_CONTINUES -1
#define XDGVFS_RESULT_DONE 0
#define XDGVFS_RESULT_OK 0

#define XDGVFS_RESULT_CANNOT_LAUNCH_BACKEND_STR "Client: Cannot launch backend"
#define XDGVFS_RESULT_CALL_SENDDATA_AGAIN_STR "Client: call sendData again"
#define XDGVFS_RESULT_GIVE_ME_DATA_STR "Client: give me data"
#define XDGVFS_RESULT_PARAMETER_ERR_STR "Client: parameter error"
#define XDGVFS_RESULT_FATAL_ERR_STR "Client: fatal error"
#define XDGVFS_RESULT_UNESCAPING_DATA_ERR_STR "Client: unescaping data error"
#define XDGVFS_RESULT_BAD_STATE_STR "Client: bad state"
#define XDGVFS_RESULT_IOERR_STR "Client: IOERR"
#define XDGVFS_RESULT_NOT_POSSIBLE_STR "Client: operation not possible" 
#define XDGVFS_RESULT_CONTINUES_STR "Client: result continues"
#define XDGVFS_RESULT_OK_STR "Client: OK/DONE"

typedef int XdgVfsResult;

/**
*  session state
*/
typedef enum {
	XDGVFS_STATE_ERROR_LEAVING = -1,
	XDGVFS_STATE_READING_DATA = 1,
	XDGVFS_STATE_WRITING_DATA = 2,
	XDGVFS_STATE_EXPECTING_PROMPT = 3,
	XDGVFS_STATE_READY = 4,
	XDGVFS_STATE_READING_TEXT = 5
} XdgVfsState;

typedef struct _XdgVfsSession XdgVfsSession;

typedef void (*XdgVfsMonitorCallback)(XdgVfsSession * sess, char * uri, void * user_data);

/*
*  Session Object 
*  never access the members of this stuct directly (it might change)!
*/
struct _XdgVfsSession {
	/* all members are private! */

	/* pipes to the sub-process */
	int fd_in;
	int fd_out;
	
	int pid;

	/* incoming data buffers */
	char * inBuf;
	int inBuf_used;
	int inBuf_size;
	XdgVfsState stateIn;

	char * rawBufIn;

	/* outgoing data buffers */
	char * outBuf;
	int outBuf_sent;
	int outBuf_used;
	int outBuf_size;
	int outBuf_EOF;
	XdgVfsState stateOut;

	/* incoming items */
	XdgVfsItemType currentItemType;
	XdgVfsItem * currentItem;

	int lastJobErrorCode;
	char * lastJobErrorText;
	
	/* file/dir change monitoring */
	XdgVfsMonitorCallback monitor_cb;
	void * monitor_cb_userdata;
	
	/* recursion protection */
	int recurse_count;

};


/**
*
*  Start Session and setup child process
*  (with a session you can run a 'session of' xdg-vfs commands (one after the other)
*  pass NULL or "this" for the current desktop or "kde"/"gnome" for preferredDesktop.
*/
XdgVfsResult xdg_vfs_sess_start(XdgVfsSession ** sessOut, char * preferredDesktop);

/**
*  Close session and kill child process
*
*/
XdgVfsResult xdg_vfs_sess_close(XdgVfsSession * sess);

/**
*  Cancel the current Command 
*
*/
XdgVfsResult xdg_vfs_sess_cancelCommand(XdgVfsSession * sess);

/**
*  Watch this fd to avoid blocking in xdgvfs_sess_readItem()
*
*/
int xdg_vfs_sess_getIncomingFiledescriptor(XdgVfsSession * sess);

/**
*  Watch this fd to avoid blocking in xdg_vfs_sess_sendData();
*
*/
int xdg_vfs_sess_getOutgoingFiledescriptor(XdgVfsSession * sess);

/**
*  Check if the session is ready for commands
*
*/
int xdg_vfs_sess_isBusy(XdgVfsSession * sess);

/* ============== file/dirwatch monitor ===================== */

void xdg_vfs_sess_set_monitor_callback(
		XdgVfsSession * sess,
		XdgVfsMonitorCallback cb,
		void * userdata
		);

/* ====================== VFS Commands =======================*/

/*
*  After running a command the session will leave the READY state and 
*  you have to read items by calling xdg_vfs_sess_readItem() (except for the 'simple' commands)
*
*  See the samples in the 'tests' directory for a tutorial.
*/

/**
*  Command: get Backend Info
*
*  returns items:
*     - XDGVFS_ITEMTYPE_BACKENDINFO
*/
XdgVfsResult xdg_vfs_sess_cmd_backendInfo(XdgVfsSession * sess);

/**
*  Command: get File
*
*  returns items:
*     - XDGVFS_ITEMTYPE_GET_HEAD
*     - XDGVFS_DATAIN (multi)
*     - XDGVFS_ITEMTYPE_DATAIN_DONE
*/
XdgVfsResult xdg_vfs_sess_cmd_getFile(XdgVfsSession * sess, const char * uri);

/**
*  Command: put File
*
*  returns items:
*     - XDGVFS_ITEMTYPE_PUT_HEAD
*     - XDGVFS_DATAOUT (multi -> you have to put data into the outgoing buffer and send it)
*     - XDGVFS_ITEMTYPE_DATAOUT_DONE
*/
XdgVfsResult xdg_vfs_sess_cmd_putFile(XdgVfsSession * sess, const char * uri, 
	XdgVfsFlags flags);

/**
*  Command: getFileInfo
*
*  returns items:
*     - XDGVFS_ITEMTYPE_FILEINFO
*/
XdgVfsResult xdg_vfs_sess_cmd_getFileInfo(XdgVfsSession * sess, const char * uri);

/**
*  Command: SetAttrs
*  
*  leave user or group NULL when you don't want to change it.
*  set setPermFlag=0 if you don't want to change the permissions.
*
*  returns items:
*     - XDGVFS_ITEMTYPE_SETATTRS_HEAD
*
*/
XdgVfsResult xdg_vfs_sess_cmd_setAttrs(XdgVfsSession * sess, 
	const char * uri, int permissions, int setPermFlag, char * user, char * group);

/**
*  Command: CopyFile
*
*  returns items:
*     - XDGVFS_ITEMTYPE_COPY_HEAD
*     - XDGVFS_ITEMTYPE_PROGRESS (multi)
*/
XdgVfsResult xdg_vfs_sess_cmd_copyFile(XdgVfsSession * sess, const char * uri_src, const char * uri_target);

/**
*  Command: Move File
*
*  returns items:
*     - XDGVFS_ITEMTYPE_MOVE_HEAD
*     - XDGVFS_ITEMTYPE_PROGRESS (multi)
*/
XdgVfsResult xdg_vfs_sess_cmd_moveFile(XdgVfsSession * sess, const char * uri_src, const char * uri_target);

/**
*  Command: Make Directory
*
*  returns items:
*     - XDGVFS_ITEMTYPE_MKDIR_HEAD
*
*/
XdgVfsResult xdg_vfs_sess_cmd_makeDirectory(XdgVfsSession * sess, const char * uri);

/**
*  Command: Remove Directory
*
*  returns items:
*     - XDGVFS_ITEMTYPE_RMDIR_HEAD
*/
XdgVfsResult xdg_vfs_sess_cmd_removeDirectory(XdgVfsSession * sess, const char * uri);

/**
*  Command: Delete File
*
*  returns items:
*     - XDGVFS_ITEMTYPE_RM_HEAD
*/
XdgVfsResult xdg_vfs_sess_cmd_removeFile(XdgVfsSession * sess, const char * uri);

/**
*  Command: List Directory
*
*  returns items:
*     - XDGVFS_ITEMTYPE_LS_HEAD
*     - XDGVFS_ITEMTYPE_FILEINFO (multi)
*/
XdgVfsResult xdg_vfs_sess_cmd_listDirectory(XdgVfsSession * sess, const char * uri);

/**
*  Command: Mount
*
*  returns items:
*     (none, but you have to loop xdg_vfs_sess_readItem() to wait for the result)
*/
XdgVfsResult xdg_vfs_sess_cmd_mount(XdgVfsSession * sess, const char * mountpoint_id);

/**
*  Command: monitor directory
*  This is a simple command, you don't have to call xdg_vfs_sess_readItem().
*/
XdgVfsResult xdg_vfs_sess_cmd_monitorDir(XdgVfsSession * sess, const char * uri);

/**
*  Command: monitor file
*  This is a simple command, you don't have to call xdg_vfs_sess_readItem().
*/
XdgVfsResult xdg_vfs_sess_cmd_monitorFile(XdgVfsSession * sess, const char * uri);

/**
*  Command: remove monitor
*  This is a simple command, you don't have to call xdg_vfs_sess_readItem().
*/
XdgVfsResult xdg_vfs_sess_cmd_removeMonitor(XdgVfsSession * sess, const char * uri);

/**
*  Command: Show Open-File-Dialog
*
*  returns items:
*     - XDGVFS_ITEMTYPE_OPENFILEDLG_RESPONSE
*/
XdgVfsResult xdg_vfs_sess_cmd_openFileDialog(XdgVfsSession * sess, const char * default_uri, XdgVfsFlags flags);

/**
*  Command: Show Save-File-Dialog
*
*  returns itmes:
*     - XDGVFS_ITEMTYPE_SAVEFILEDLG_RESPONSE
*/
XdgVfsResult xdg_vfs_sess_cmd_saveFileDialog(XdgVfsSession * sess, 
	const char * default_folder_uri, 
	const char * default_filename, 
	XdgVfsFlags flags);

/* ============= READ / WRITE FUNCTIONS ============= */

/**
*  Read an Item or a Chunk of File-Data. Continue reading items until
*  the return value is != XDGVFS_RESULT_CONTINUES.
*
*  This function has to be looped after executing a xdg-vfs command (but not for the 'simple' commands).
*  
*  If you want to do this non-blocking check the incoming fd in your event-loop.
*/
XdgVfsResult xdg_vfs_sess_readItem(XdgVfsSession * sess, XdgVfsItemType * typeOut, 
		XdgVfsItem ** itemOut, char ** buffer, int * len);


/* ------ used when writing a file with xdg_vfs_sess_cmd_putFile() -------- */

/**
*  Put a Chunk of File-Data into the outgoing buffer.
*  set len = 0 on EOF. 
*  If there is no error this function will usually return XDGVFS_RESULT_CALL_SENDDATA_AGAIN 
*  -> You have to call the function xdg_vfs_sess_sendData() until the outgoing 
*  buffer is emptied.
*/
XdgVfsResult xdg_vfs_sess_putDataChunk(XdgVfsSession * sess, char * data, int len);

/**
*  Send data from the outgoing buffer - repeat this on 
*  XDGVFS_RESULT_CALL_SENDDATA_AGAIN until you get 
*  XDGVFS_RESULT_GIVE_ME_DATA or XDGVFS_RESULT_OK (finished);
* 
*  if you want to do that nonblocking check the outgoing fd first
*/
XdgVfsResult xdg_vfs_sess_sendData(XdgVfsSession * sess);



/* ================= XdgVfsItem  ======================*/

/**
*  increase reference Count
*
*/
XdgVfsItem * xdg_vfs_item_ref(XdgVfsItem * item);

/**
*  decrease reference Count
*
*/
void xdg_vfs_item_unref(XdgVfsItem * item);

/* =================== tools ========================= */

/*
*  get string message for XdgVfsResult. The string is statically allocated, you don't have to free() it.
*/
char * xdg_vfs_resultToString(XdgVfsResult code);


#ifdef __cplusplus
}
#endif
#endif