1 /*----------------------------------------------------------------------------
2 --
3 --  Module:           xtmDbTools
4 --
5 --  Project:          Xdiary
6 --  System:           xtm - X Desktop Calendar
7 --    Subsystem:      <>
8 --    Function block: <>
9 --
10 --  Description:
11 --    Interface file for module xtmDbTools
12 --
13 --  Filename:         xtmDbTools.h
14 --
15 --  Authors:          Roger Larsson, Ulrika Bornetun
16 --  Creation date:    1991-05-28
17 --
18 --
19 --  (C) Copyright Ulrika Bornetun, Roger Larsson (1995)
20 --      All rights reserved
21 --
22 --  Permission to use, copy, modify, and distribute this software and its
23 --  documentation for any purpose and without fee is hereby granted,
24 --  provided that the above copyright notice appear in all copies. Ulrika
25 --  Bornetun and Roger Larsson make no representations about the usability
26 --  of this software for any purpose. It is provided "as is" without express
27 --  or implied warranty.
28 ----------------------------------------------------------------------------*/
29 
30 /* SCCS module identifier. */
31 /* SCCSID = @(#) Module: xtmDbTools.h, Version: 1.2, Date: 95/06/17 12:51:38 */
32 
33 
34 /* Only include this module once. */
35 #ifndef define_xtmDbTools_h
36 #  define define_xtmDbTools_h
37 
38 
39 /*----------------------------------------------------------------------------
40 --  Include files
41 ----------------------------------------------------------------------------*/
42 
43 #include <stdio.h>
44 #include <string.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 
48 #include "xdndbm.h"
49 #include "Standard.h"
50 #include "LstLinked.h"
51 #include "TimDate.h"
52 
53 
54 /*----------------------------------------------------------------------------
55 --  Macro definitions
56 ----------------------------------------------------------------------------*/
57 
58 /* Public file names. */
59 #define  XTM_DB_MESSAGE_FILE    "msg"
60 
61 /* Max entries per day. */
62 #define XTM_DB_DATE_ID_SIZE     50
63 
64 /* Max length for text in the entry record. */
65 #define XTM_DB_RECORD_TEXT_LEN  100
66 
67 /* Max length for entry tag. */
68 #define XTM_DB_TAG_LEN          4
69 
70 /* Max number of records in log file. */
71 #define XTM_DB_MAX_LOG_RECORDS  200
72 
73 /* Version number for messages. */
74 #define XTM_DB_MESSAGE_VERSION  1
75 
76 /* Flags when fetching entries. */
77 #define XTM_DB_FETCH_STANDING              (1<<0)
78 #define XTM_DB_FETCH_STICKY                (1<<1)
79 #define XTM_DB_FETCH_ALL_STANDING          (1<<2)
80 #define XTM_DB_FETCH_NO_NEW_LIST           (1<<3)
81 #define XTM_DB_FETCH_INCLUDE               (1<<4)
82 #define XTM_DB_FETCH_ALL_TEXT              (1<<5)
83 #define XTM_DB_FETCH_SKIP_INCLUDE          (1<<6)
84 
85 /* Flags defined for an entry. */
86 #define XTM_DB_FLAG_ALARM                  (1<<0)
87 #define XTM_DB_FLAG_IMPORTANT              (1<<1)
88 #define XTM_DB_FLAG_NOTE_DONE              (1<<2)
89 #define XTM_DB_FLAG_EXT_FILE               (1<<3)
90 #define XTM_DB_FLAG_HIDE_IN_DAY_VIEW       (1<<4)
91 #define XTM_DB_FLAG_ARCHIVED               (1<<5)
92 #define XTM_DB_FLAG_PRIVATE                (1<<6)
93 #define XTM_DB_FLAG_HIDE_IN_CALENDAR       (1<<7)
94 #define XTM_DB_FLAG_HIDE_IN_SUMMARY        (1<<8)
95 #define XTM_DB_FLAG_HIDE_IN_PRINT          (1<<9)
96 #define XTM_DB_FLAG_INCLUDE                (1<<10)
97 #define XTM_DB_FLAG_IN_PRIV_DB             (1<<11)
98 #define XTM_DB_FLAG_PRIV_EXT_FILE          (1<<12)
99 #define XTM_DB_FLAG_HIDE_IN_DUMP           (1<<13)
100 #define XTM_DB_FLAG_HIDE_IN_ALARM          (1<<14)
101 #define XTM_DB_FLAG_ONLY_OWNER_DELETE      (1<<15)
102 #define XTM_DB_FLAG_ONLY_OWNER_CHANGE      (1<<16)
103 #define XTM_DB_FLAG_KEEP_IN_BG             (1<<17)
104 #define XTM_DB_FLAG_KEEP_IN_FG             (1<<18)
105 
106 #define XTM_DB_FLAG_ACTION_SCRIPT          (1<<20)
107 #define XTM_DB_FLAG_ACTION_TEXT            (1<<21)
108 #define XTM_DB_FLAG_ACTION_ALARM1          (1<<22)
109 #define XTM_DB_FLAG_ACTION_ALARM2          (1<<23)
110 #define XTM_DB_FLAG_ACTION_ALARM3          (1<<24)
111 #define XTM_DB_FLAG_ACTION_ALARM4          (1<<25)
112 #define XTM_DB_FLAG_ACTION_ALARM5          (1<<26)
113 
114 /* Flags defined for a standing entry. */
115 #define XTM_DB_FLAG_SE_STANDING            (1<<0)
116 #define XTM_DB_FLAG_SE_STICKY              (1<<1)
117 #define XTM_DB_FLAG_SE_HIDE_IN_CALENDAR    (1<<2)
118 #define XTM_DB_FLAG_SE_DAY_IN_MONTH        (1<<3)
119 #define XTM_DB_FLAG_SE_1ST                 (1<<4)
120 #define XTM_DB_FLAG_SE_2ND                 (1<<5)
121 #define XTM_DB_FLAG_SE_3RD                 (1<<6)
122 #define XTM_DB_FLAG_SE_1ST_LAST            (1<<7)
123 #define XTM_DB_FLAG_SE_2ND_LAST            (1<<8)
124 #define XTM_DB_FLAG_SE_3RD_LAST            (1<<9)
125 #define XTM_DB_FLAG_SE_NWDAY_PREV          (1<<10)
126 #define XTM_DB_FLAG_SE_NWDAY_NEXT          (1<<11)
127 #define XTM_DB_FLAG_SE_NWDAY_SKIP          (1<<12)
128 
129 /* Flags for database operations. */
130 #define XTM_DB_FLAG_MODE_READ              (1<<0)
131 #define XTM_DB_FLAG_MODE_WRITE             (1<<1)
132 #define XTM_DB_FLAG_MODE_PRIV              (1<<2)
133 #define XTM_DB_FLAG_MODE_MSG               (1<<3)
134 #define XTM_DB_FLAG_MODE_SETID             (1<<8)
135 
136 /* Flags for log records. */
137 #define XTM_DB_FLAG_LOG_SAVE               (1<<0)
138 #define XTM_DB_FLAG_LOG_DELETE             (1<<1)
139 #define XTM_DB_FLAG_LOG_COPY               (1<<2)
140 #define XTM_DB_FLAG_LOG_MOVE               (1<<3)
141 
142 /* Flags for protection. */
143 #define XTM_DB_PROT_READ                   (1<<0)
144 #define XTM_DB_PROT_WRITE                  (1<<1)
145 #define XTM_DB_PROT_DELETE                 (1<<2)
146 #define XTM_DB_PROT_CHANGE                 (1<<3)
147 
148 /* Name of key and data files. */
149 #define  ENTRY_DB_NAME             "idDB"
150 #define  ENTRY_DB_NAME_DIR         "idDB.dir"
151 #define  ENTRY_DB_NAME_PAG         "idDB.pag"
152 
153 #define  DATE_DB_NAME              "dateDB"
154 #define  DATE_DB_NAME_DIR          "dateDB.dir"
155 #define  DATE_DB_NAME_PAG          "dateDB.pag"
156 
157 #define  STAND_ENTRY_DB_NAME       "standDB"
158 #define  STAND_ENTRY_DB_NAME_DIR   "standDB.dir"
159 #define  STAND_ENTRY_DB_NAME_PAG   "standDB.pag"
160 
161 #define  PRIV_ENTRY_DB_NAME        "Private/privDB"
162 #define  PRIV_ENTRY_DB_NAME_DIR    "Private/privDB.dir"
163 #define  PRIV_ENTRY_DB_NAME_PAG    "Private/privDB.pag"
164 
165 #define  ID_FILE                   "entryId"
166 #define  LOG_FILE                  "dbLog"
167 #define  LOCK_FILE                 "dbLock"
168 
169 
170 /*----------------------------------------------------------------------------
171 --  Type declarations
172 ----------------------------------------------------------------------------*/
173 
174 /* Status from database calls. */
175 typedef enum {
176   XTM_DB_OK, XTM_DB_ERROR, XTM_DB_UNKNOWN, XTM_DB_LOCKED
177 } XTM_DB_STATUS;
178 
179 
180 /* Identifiers for the databases managed. */
181 typedef enum {
182   XTM_DB_ENTRY_DB,
183   XTM_DB_DATE_DB,
184   XTM_DB_STAND_ENTRY_DB,
185   XTM_DB_PRIV_ENTRY_DB,
186   XTM_DB_ALL_ENTRY_DB
187 } XTM_DB_DATABASES;
188 
189 
190 /* Standing is valid every: */
191 typedef enum {
192   XTM_DB_VALID_NONE,
193   XTM_DB_VALID_DAY,
194   XTM_DB_VALID_WEEK,
195   XTM_DB_VALID_MONTH,
196   XTM_DB_VALID_YEAR,
197   XTM_DB_VALID_MONTH_LAST
198 } XTM_DB_STAND_VALID;
199 
200 
201 /* Type of entry. */
202 typedef enum {
203   XTM_DB_DAY_NOTE,
204   XTM_DB_DAY_ENTRY,
205   XTM_DB_UNDEFINED,
206   XTM_DB_ALL_ENTRIES
207 } XTM_DB_ENTRY_TYPE;
208 
209 /* Category for entries. */
210 typedef enum {
211   XTM_DB_ENTRY_LIST,
212   XTM_DB_REP_ENTRY_LIST,
213   XTM_DB_STICKY_LIST
214 } XTM_DB_ENTRY_CATEGORY;
215 
216 
217 /* The different tyoes of entries. */
218 typedef enum {
219   XTM_DB_TYPE_APPOINTMENT,
220   XTM_DB_TYPE_STAND_APPOINTMENT,
221   XTM_DB_TYPE_NOTE,
222   XTM_DB_TYPE_STAND_NOTE,
223   XTM_DB_TYPE_STICKY_NOTE
224 } XTM_DB_ALL_ENTRY_TYPES;
225 
226 
227 /* Reference to database. */
228 typedef DBM  *XTM_DB_DATABASE;
229 
230 
231 /* Request to create a database in XDiary. */
232 typedef struct {
233   char              *directory;
234   XTM_DB_DATABASES  database;
235 } XTM_DB_CREATE_REQUEST;
236 
237 
238 /* Request to open a database in XDiary. */
239 typedef struct {
240   char              *name;
241   char              *directory;
242   UINT32            operations;
243   int               lock_timeout;
244   int               uid_locking;
245   XTM_DB_DATABASES  database;
246 } XTM_DB_OPEN_REQUEST;
247 
248 
249 /* Request for a entry id. */
250 typedef struct {
251   char     *directory;
252   UINT32   operations;
253   Boolean  lock_file;
254 } XTM_DB_ID_REQUEST;
255 
256 
257 /* Header record in log file. */
258 typedef struct {
259   UINT32  last_changed;
260   UINT32  current_ref;
261   UINT32  records;
262   UINT32  max_log_records;
263   char    unused[ 50 - 4 * 4 ];
264 } XTM_DB_LOG_HEADER;
265 
266 
267 /* Log record in log file. */
268 typedef struct {
269   UINT32  time_stamp;
270   UINT32  entry_id;
271   UINT32  flags;
272   UINT32  changed_by;
273 } XTM_DB_LOG_RECORD;
274 
275 
276 /* Database references for entries (all databases). */
277 typedef struct {
278   char             database_dir[ 150 ];
279   char             name[ 16 ];
280   XTM_DB_DATABASE  entry_db;
281   XTM_DB_DATABASE  date_db;
282   XTM_DB_DATABASE  stand_entry_db;
283   XTM_DB_DATABASE  private_db;
284   UINT32           operations;
285   int              lock_fd;
286   Boolean          locked;
287   Boolean          set_id;
288 } XTM_DB_ENTRY_DATABASES;
289 
290 
291 /* Database references for messages. */
292 typedef struct {
293   char      database_dir[ 150 ];
294   int       lock_fd;
295   Boolean   locked;
296   Boolean   set_id;
297 } XTM_DB_MESSAGE_DATABASE;
298 
299 
300 /* XDiary message record. */
301 typedef struct {
302   UINT8   version;
303   char    from[ 21 ];
304   UINT32  flags;
305   UINT32  date;
306   UINT32  time;
307   UINT16  duration;
308   UINT16  message_length;
309   UINT16  text_length;
310 } XTM_DB_MESSAGE_DEF, *XTM_DB_MESSAGE_REF;
311 
312 
313 /* Key and record containing data for an entry. */
314 typedef struct {
315   UINT32  id;
316 } XTM_DB_ENTRY_KEY;
317 
318 typedef struct {
319   UINT32  id;
320   UINT32  time_stamp;
321   UINT32  date_stamp;
322   UINT32  last_update;
323   UINT32  owner;
324   UINT8   entry_type;
325   UINT8   entry_category;
326   char    tag[ XTM_DB_TAG_LEN + 1 ];
327   UINT16  duration;
328   UINT32  flags;
329   INT16   alarm_offset[ 5 ];
330   UINT8   alarm_valid[ 5 ];
331   UINT8   alarm_melody;
332   UINT8   day_list_lines;
333   UINT8   alarm_lines;
334   char    text[ XTM_DB_RECORD_TEXT_LEN + 1 ];
335   UINT8   fg_color_index;
336   UINT8   bg_color_index;
337   INT32   crc;
338 
339   char    unused[ 170 - 7*4 - 1*2 - 5*2 - 5*1 - 7*1 -
340                   XTM_DB_TAG_LEN - 1 -
341                   XTM_DB_RECORD_TEXT_LEN - 1 ];
342 } XTM_DB_ENTRY_DEF, *XTM_DB_ENTRY_REF;
343 
344 
345 /* Key and record containing entries for a day. */
346 typedef struct {
347   UINT32  date;
348 } XTM_DB_DATE_KEY;
349 
350 typedef struct {
351   UINT32  date;
352   UINT32  id[ XTM_DB_DATE_ID_SIZE ];
353 
354   char   unused[ 256 - 1*4 - XTM_DB_DATE_ID_SIZE*4 ];
355 } XTM_DB_DATE_DEF, *XTM_DB_DATE_REF;
356 
357 
358 /* Key and record for standing entries. */
359 typedef struct {
360   UINT32  id;
361 } XTM_DB_STAND_ENTRY_KEY;
362 
363 typedef struct {
364   UINT32  id;
365   UINT32  from;
366   UINT32  to;
367   UINT32  flags;
368   UINT8   valid_days[ 7 ];
369   UINT16  every_n;
370   UINT8   valid_every;
371   UINT32  skip_week[ 2 ];
372 
373   char    unused[ 48 - 4*4 - 2*4 - 7*1 - 1*2 - 1*1 ];
374 } XTM_DB_STAND_ENTRY_DEF, *XTM_DB_STAND_ENTRY_REF;
375 
376 
377 /* Key and record for private data. */
378 typedef struct {
379   UINT32  id;
380 } XTM_DB_PRIV_KEY;
381 
382 typedef struct {
383   UINT32  id;
384   char    text[ XTM_DB_RECORD_TEXT_LEN + 1 ];
385 
386   char    unused[ 110 - 1 * 4 - XTM_DB_RECORD_TEXT_LEN - 1 ];
387 } XTM_DB_PRIV_DEF, *XTM_DB_PRIV_REF;
388 
389 
390 /* Record describng one entry (a combination of many records). */
391 typedef struct {
392   XTM_DB_ENTRY_DEF        entry;
393   XTM_DB_STAND_ENTRY_DEF  stand_entry;
394   char                    db_name[ 16 ];
395   char                    *all_text;
396 } XTM_DB_ALL_ENTRY_DEF, *XTM_DB_ALL_ENTRY_REF;
397 
398 
399 /*----------------------------------------------------------------------------
400 --  Global definitions
401 ----------------------------------------------------------------------------*/
402 
403 
404 /*----------------------------------------------------------------------------
405 --  Function prototypes
406 ----------------------------------------------------------------------------*/
407 
408 XTM_DB_STATUS
409   xtmDbChangesInLog( char               *directory,
410                      TIM_TIME_REF       since,
411                      XTM_DB_LOG_RECORD  changes[],
412                      int                max_changes,
413                      int                *no_changes );
414 
415 void
416   xtmDbCheckDbOperations( char     *directory,
417                           Boolean  force_check,
418                           UINT32   *operations );
419 
420 void
421   xtmDbClearAccessCache();
422 
423 XTM_DB_STATUS
424   xtmDbCloseEntryDb( XTM_DB_ENTRY_DATABASES *entry_databases );
425 
426 XTM_DB_STATUS
427   xtmDbCreateDatabase( XTM_DB_CREATE_REQUEST  *create_request );
428 
429 XTM_DB_STATUS
430   xtmDbDeleteEntry( XTM_DB_ENTRY_DATABASES  *databases,
431                     UINT32                  id );
432 
433 XTM_DB_STATUS
434   xtmDbDeleteMessage( char    *db_dir,
435                       UINT32  msg_id );
436 
437 Boolean
438   xtmDbDoesStandingMatch( XTM_DB_STAND_ENTRY_REF  stand_ref,
439                           UINT32                  flags,
440                           Boolean                 check_non_wday,
441                           TIM_TIME_REF            date );
442 
443 XTM_DB_STATUS
444   xtmDbFetchDates( XTM_DB_ENTRY_DATABASES  *databases,
445                    LST_DESC_TYPE           *list_ref );
446 
447 XTM_DB_STATUS
448   xtmDbFetchEntry( XTM_DB_ENTRY_DATABASES  *databases,
449                    UINT32                  id,
450                    XTM_DB_ALL_ENTRY_REF    entry_ref,
451                    char                    **text_ref );
452 
453 XTM_DB_STATUS
454   xtmDbFetchEntriesInDay( XTM_DB_ENTRY_DATABASES  *databases,
455                           TIM_TIME_REF            date,
456                           UINT32                  flags,
457                           LST_DESC_TYPE           *entry_list_ref,
458                           LST_DESC_TYPE           *note_list_ref );
459 
460 XTM_DB_STATUS
461   xtmDbFetchFileInfo( char         *filename,
462                       struct stat  *file_info );
463 
464 XTM_DB_STATUS
465   xtmDbFetchMessage( char                *db_dir,
466                      UINT32              msg_id,
467                      XTM_DB_MESSAGE_REF  msg_info,
468                      char                **message,
469                      char                **text );
470 
471 XTM_DB_STATUS
472   xtmDbFetchStandEntries( XTM_DB_ENTRY_DATABASES  *databases,
473                           LST_DESC_TYPE           *entry_list_ref,
474                           LST_DESC_TYPE           *note_list_ref );
475 
476 XTM_DB_STATUS
477   xtmDbGenerateId( XTM_DB_ID_REQUEST  *request,
478                    UINT32             *id );
479 
480 void
481   xtmDbGetEntryPermissions( UINT32  db_operations,
482                             int     entry_owner,
483                             UINT32  entry_flags,
484                             UINT32  *can_do_flags );
485 
486 XTM_DB_STATUS
487   xtmDbInitializeAuxFiles( char  *directory );
488 
489 void
490   xtmDbInitializeProcessId();
491 
492 XTM_DB_STATUS
493   xtmDbInsertEntry( XTM_DB_ENTRY_DATABASES  *databases,
494                     XTM_DB_ALL_ENTRY_REF    entry_ref,
495                     char                    *text_ref );
496 
497 XTM_DB_STATUS
498   xtmDbInsertMessage( char                *db_dir,
499                       XTM_DB_MESSAGE_REF  msg_info,
500                       char                *message,
501                       char                *text );
502 
503 Boolean
504   xtmDbIsEntryDefined( XTM_DB_ENTRY_DATABASES  *database,
505                        LST_DESC_TYPE           *stand_entries,
506                        TIM_TIME_REF            cal_date );
507 
508 XTM_DB_STATUS
509   xtmDbOpenEntryDb( XTM_DB_OPEN_REQUEST     *open_request,
510                     XTM_DB_ENTRY_DATABASES  *entry_databases );
511 
512 void
513   xtmDbUseFileLock( Boolean  use_file_lock );
514 
515 
516 
517 /*----------------------------------------------------------------------------
518 --  Functions
519 ----------------------------------------------------------------------------*/
520 
521 #endif
522