1 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2   backendn-scrn.c defines the notes screen output backend of refdbd
3   markus@mhoenicka.de 2003-10-14
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9 
10    This program is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with this program; if not, write to the Free Software
17    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 
19   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
20 
21 #include <string.h>
22 #include <stdio.h>
23 #include <syslog.h> /* for definitions of log message priorities */
24 #include <time.h>
25 #include <sys/types.h> /* for getpid() */
26 #include <unistd.h>    /* for getpid() */
27 #include <dbi/dbi.h>
28 
29 #include "refdb.h"
30 #include "backend.h"
31 #include "backendn-scrn.h"
32 #include "strfncs.h"
33 #include "dbfncs.h"
34 #include "linklist.h"
35 #include "refdbd.h"
36 #include "connect.h"
37 
38 /* some globals */
39 extern int n_log_level; /* numeric version of log_level */
40 
41 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
42   prepare_render_note_scrn(): writes a header for the screen output of a
43                          notes query
44 
45   int prepare_render_note_scrn returns 0 if successful, >0 if failed
46 
47   struct renderinfo* ptr_rendinfo ptr to a structure with the info
48                              how the note should be rendered
49 
50  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
prepare_render_note_scrn(struct renderinfo * ptr_rendinfo)51 int prepare_render_note_scrn(struct renderinfo* ptr_rendinfo) {
52   /* we just make sure that we start with a clean string */
53   (*(ptr_rendinfo->ptr_ref))[0] = '\0';
54   return 0;
55 }
56 
57 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
58   finish_render_note_scrn(): writes a footer for the screen output of
59                              a notes query
60 
61   int finish_render_note_scrn returns 0 if successful, >0 if failed
62 
63   struct renderinfo* ptr_rendinfo ptr to a structure with the info
64                              how the reference should be rendered
65 
66  ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
finish_render_note_scrn(struct renderinfo * ptr_rendinfo)67 int finish_render_note_scrn(struct renderinfo* ptr_rendinfo) {
68   /* nothing to do here */
69   return 0;
70 }
71 
72 /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
73   render_note_scrn() renders a note for screen display
74 
75   int render_note_scrn returns 0 if successful, >0 if failed
76 
77   struct renderinfo* ptr_rendinfo ptr to a structure with the info
78                              how the reference should be rendered
79 
80   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
render_note_scrn(struct renderinfo * ptr_rendinfo)81 int render_note_scrn(struct renderinfo* ptr_rendinfo) {
82   int i;
83   char id[32] = "";
84   char date_buffer[256];
85   char linktype[4][11] = {"reference", "keyword", "author", "periodical"};
86   const char* citem;
87   char* item;
88   char* new_ref;
89   dbi_conn conn;
90   dbi_result dbires;
91 
92   conn = dbi_result_get_conn(ptr_rendinfo->dbires);
93 
94   /*----------------------------------------------------------------*/
95   /* ID */
96   get_refdb_note_id(ptr_rendinfo->dbires, id);
97 
98   if (*id) {
99     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "Note ID:", ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
100       LOG_PRINT(LOG_CRIT, get_status_msg(801));
101       return 801;
102     }
103     else {
104       *(ptr_rendinfo->ptr_ref) = new_ref;
105     }
106 
107     /* ID */
108     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), id, ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
109       LOG_PRINT(LOG_CRIT, get_status_msg(801));
110       return 801;
111     }
112     else {
113       *(ptr_rendinfo->ptr_ref) = new_ref;
114     }
115   }
116   else {
117     LOG_PRINT(LOG_WARNING, get_status_msg(234));
118     return 234; /* this is bad and will hopefully never happen */
119   }
120 
121   /*----------------------------------------------------------------*/
122   /* date */
123   if (get_refdb_note_date(ptr_rendinfo->dbires, date_buffer, 0) != NULL) {
124     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), " (", ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
125       LOG_PRINT(LOG_CRIT, get_status_msg(801));
126       return 801;
127     }
128     else {
129       *(ptr_rendinfo->ptr_ref) = new_ref;
130     }
131 
132     /* date */
133     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), date_buffer, ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
134       LOG_PRINT(LOG_CRIT, get_status_msg(801));
135       return 801;
136     }
137     else {
138       *(ptr_rendinfo->ptr_ref) = new_ref;
139     }
140 
141     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), ")", ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
142       LOG_PRINT(LOG_CRIT, get_status_msg(801));
143       return 801;
144     }
145     else {
146       *(ptr_rendinfo->ptr_ref) = new_ref;
147     }
148   }
149 
150   if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "\n", ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
151     LOG_PRINT(LOG_CRIT, get_status_msg(801));
152     return 801;
153   }
154   else {
155     *(ptr_rendinfo->ptr_ref) = new_ref;
156   }
157 
158   /*----------------------------------------------------------------*/
159   /* note key */
160   citem = get_refdb_note_key(ptr_rendinfo->dbires);
161   if (citem && *citem) {
162     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "Key: ", ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
163       LOG_PRINT(LOG_CRIT, get_status_msg(801));
164       return 801;
165     }
166     else {
167       *(ptr_rendinfo->ptr_ref) = new_ref;
168     }
169 
170     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), (char*)citem, ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
171       LOG_PRINT(LOG_CRIT, get_status_msg(801));
172       return 801;
173     }
174     else {
175       *(ptr_rendinfo->ptr_ref) = new_ref;
176     }
177 
178     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "\n", ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
179       LOG_PRINT(LOG_CRIT, get_status_msg(801));
180       return 801;
181     }
182     else {
183       *(ptr_rendinfo->ptr_ref) = new_ref;
184     }
185   }
186 
187   /*----------------------------------------------------------------*/
188   /* note title */
189   item = get_refdb_note_title_copy(ptr_rendinfo->dbires);
190   if (item != NULL) {
191     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), (char*)item, ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* title */
192       free(item);
193       LOG_PRINT(LOG_CRIT, get_status_msg(801));
194       return 801;
195     }
196     else {
197       *(ptr_rendinfo->ptr_ref) = new_ref;
198     }
199 
200     free(item);
201 
202     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "\n", ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
203       LOG_PRINT(LOG_CRIT, get_status_msg(801));
204       return 801;
205     }
206     else {
207       *(ptr_rendinfo->ptr_ref) = new_ref;
208     }
209   }
210 
211   /*----------------------------------------------------------------*/
212   /* the note proper */
213   item = get_refdb_note_content_copy(ptr_rendinfo->dbires);
214   if (item != NULL) {
215     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), (char*)item, ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
216       free(item);
217       LOG_PRINT(LOG_CRIT, get_status_msg(801));
218       return 801;
219     }
220     else {
221       *(ptr_rendinfo->ptr_ref) = new_ref;
222     }
223 
224     free(item);
225 
226     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "\n", ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
227       LOG_PRINT(LOG_CRIT, get_status_msg(801));
228       return 801;
229     }
230     else {
231       *(ptr_rendinfo->ptr_ref) = new_ref;
232     }
233   }
234 
235   /*----------------------------------------------------------------*/
236   /* links */
237 
238   if (strstr((ptr_rendinfo->ptr_biblio_info)->format_string, "NL") != NULL
239       || strstr((ptr_rendinfo->ptr_biblio_info)->format_string, "ALL") != NULL) {
240     if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "\nNote is attached to:", ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
241       LOG_PRINT(LOG_CRIT, get_status_msg(801));
242       return 801;
243     }
244     else {
245       *(ptr_rendinfo->ptr_ref) = new_ref;
246     }
247 
248     /* loop over reference, keyword, author, periodical links */
249     for (i = 0; i < 4; i++) {
250       int mode = i;
251       dbires = request_links(conn, my_dbi_result_get_idval(ptr_rendinfo->dbires, "note_id"), i);
252       if (dbires == NULL) {
253 	return 234;
254       }
255 
256       /* fetch all links of this type and note */
257       while ((citem = get_link(dbires, &mode)) != NULL) {
258 	if (i == 3) { /* periodical */
259 	  if (!mode) { /* full name */
260 	    if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "\nPeriodical full name: ", ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
261 	      free(item);
262 	      LOG_PRINT(LOG_CRIT, get_status_msg(801));
263 	      return 801;
264 	    }
265 	    else {
266 	      *(ptr_rendinfo->ptr_ref) = new_ref;
267 	    }
268 	    if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), (char*)citem, ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
269 	      free(item);
270 	      LOG_PRINT(LOG_CRIT, get_status_msg(801));
271 	      return 801;
272 	    }
273 	    else {
274 	      *(ptr_rendinfo->ptr_ref) = new_ref;
275 	    }
276 	}
277 	  else if (mode == 1) { /* abbrev */
278 	    if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "\nPeriodical abbreviation: ", ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
279 	      free(item);
280 	      LOG_PRINT(LOG_CRIT, get_status_msg(801));
281 	      return 801;
282 	    }
283 	    else {
284 	      *(ptr_rendinfo->ptr_ref) = new_ref;
285 	    }
286 	    if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), (char*)citem, ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
287 	      free(item);
288 	      LOG_PRINT(LOG_CRIT, get_status_msg(801));
289 	      return 801;
290 	    }
291 	    else {
292 	      *(ptr_rendinfo->ptr_ref) = new_ref;
293 	    }
294 	  }
295 	  else if (mode == 2) { /* custabbrev 1 */
296 	    if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "\nPeriodical custom abbreviation 1: ", ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
297 	      free(item);
298 	      LOG_PRINT(LOG_CRIT, get_status_msg(801));
299 	      return 801;
300 	    }
301 	    else {
302 	      *(ptr_rendinfo->ptr_ref) = new_ref;
303 	    }
304 	    if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), (char*)citem, ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
305 	      free(item);
306 	      LOG_PRINT(LOG_CRIT, get_status_msg(801));
307 	      return 801;
308 	    }
309 	    else {
310 	      *(ptr_rendinfo->ptr_ref) = new_ref;
311 	    }
312 	  }
313 	  else if (mode == 3) { /* custabbrev 2 */
314 	    if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "\nPeriodical custom abbreviation 2: ", ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
315 	      free(item);
316 	      LOG_PRINT(LOG_CRIT, get_status_msg(801));
317 	      return 801;
318 	    }
319 	    else {
320 	      *(ptr_rendinfo->ptr_ref) = new_ref;
321 	    }
322 	    if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), (char*)citem, ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
323 	      free(item);
324 	      LOG_PRINT(LOG_CRIT, get_status_msg(801));
325 	      return 801;
326 	    }
327 	    else {
328 	      *(ptr_rendinfo->ptr_ref) = new_ref;
329 	    }
330 	  }
331 	}
332 	else {
333 	  /* reuse date_buffer */
334 	  sprintf(date_buffer, "\n%s: ", linktype[i]);
335 	  if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), date_buffer, ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
336 	    free(item);
337 	    LOG_PRINT(LOG_CRIT, get_status_msg(801));
338 	    return 801;
339 	  }
340 	  else {
341 	    *(ptr_rendinfo->ptr_ref) = new_ref;
342 	  }
343 	  if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), (char*)citem, ptr_rendinfo->ptr_ref_len, 0)) == NULL) { /* note proper */
344 	    free(item);
345 	    LOG_PRINT(LOG_CRIT, get_status_msg(801));
346 	    return 801;
347 	  }
348 	  else {
349 	    *(ptr_rendinfo->ptr_ref) = new_ref;
350 	  }
351 	}
352       }
353       clean_request(dbires);
354     } /* end for */
355   }
356 
357   /*----------------------------------------------------------------*/
358   /* ulink */
359   if (strstr((ptr_rendinfo->ptr_biblio_info)->format_string, "UR") != NULL
360       || strstr((ptr_rendinfo->ptr_biblio_info)->format_string, "ALL") != NULL) {
361     int i;
362     char link_tag[12];
363 
364     /* loop over all link types */
365     for (i=0; i<5;i++) {
366       if (!i) {
367 	strcpy(link_tag, "\nURL: ");
368       }
369       else if (i == 1) {
370 	strcpy(link_tag, "\nPDF: ");
371       }
372       else if (i == 2) {
373 	strcpy(link_tag, "\nFULLTEXT: ");
374       }
375       else if (i == 3) {
376 	strcpy(link_tag, "\nRELATED: ");
377       }
378       else if (i == 4) {
379 	strcpy(link_tag, "\nIMAGE: ");
380       }
381 
382       dbires = request_ulinks(conn, my_dbi_result_get_idval(ptr_rendinfo->dbires, "note_id"), 1 /* note entry */, i /* link type */, 0 /* is_temp */, ptr_rendinfo->username);
383       if (dbires == NULL) {
384 	return 234;
385       }
386 
387       while ((citem = get_ulink(dbires)) != NULL) {
388 	char* full_link;
389 
390 	if (i>0 && i<5) {
391 	  full_link = add_root_to_link(citem, ptr_rendinfo->pdfroot);
392 	}
393 	else {
394 	  full_link = strdup(citem);
395 	}
396 
397 	if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), link_tag, ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
398 	  LOG_PRINT(LOG_CRIT, get_status_msg(801));
399 	  clean_request(dbires);
400 	  return 801;
401 	}
402 	else {
403 	  *(ptr_rendinfo->ptr_ref) = new_ref;
404 	}
405 
406 	if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), full_link, ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
407 	  LOG_PRINT(LOG_CRIT, get_status_msg(801));
408 	  clean_request(dbires);
409 	  return 801;
410 	}
411 	else {
412 	  *(ptr_rendinfo->ptr_ref) = new_ref;
413 	}
414 
415 	free(full_link);
416 
417       } /* end while */
418       clean_request(dbires);
419     } /* end for */
420   }
421 
422   /* finish with an empty line */
423   if ((new_ref = mstrcat(*(ptr_rendinfo->ptr_ref), "\n\n", ptr_rendinfo->ptr_ref_len, 0)) == NULL) {
424     LOG_PRINT(LOG_CRIT, get_status_msg(801));
425     return 801;
426   }
427   else {
428     *(ptr_rendinfo->ptr_ref) = new_ref;
429   }
430 /*      printf("%s\n", ref); */
431   return 0;
432 }
433