aboutsummaryrefslogtreecommitdiff
path: root/src/plugins/fs/helper.c
blob: 9f6eaed9e4fb2d71de3eae4187187fe514903196 (plain) (blame)
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
/*
     This file is part of GNUnet.
     (C) 2005, 2006 Christian Grothoff (and other contributing authors)

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

     GNUnet 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
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with GNUnet; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
*/

/**
 * @file src/plugins/fs/helper.c
 * @brief helper functions (parsing)
 * @author Christian Grothoff
 */

#include "platform.h"
#include "helper.h"

/**
 * Parse a time given in the form
 * "XX seconds yy days zz months".
 *
 * @param val set to the computed time
 * @return OK on success, SYSERR on error
 */
int parseTime(const char * t,
	      TIME_T * param) {
  int pos;
  int start;
  unsigned int val;
  char * tmp;
  cron_t ret;

  ret = 0;
  pos = 0;

  while (t[pos] != '\0') {
    start = pos;
    while ( (t[pos] != ' ') &&
	    (t[pos] != '\0') )
      pos++;
    tmp = STRNDUP(&t[start],
		  pos - start);
    if (1 != sscanf(tmp,
		    "%u",
		    &val) )
      return -1; /* parse error */
    FREE(tmp);
    while ( t[pos] == ' ')
      pos++;
    start = pos;
    while ( (t[pos] != ' ') &&
	    (t[pos] != '\0') )
      pos++;
    if (0 == strncasecmp(&t[start],
			 _("ms"),
			 strlen(_("ms"))))
      ret += cronMILLIS * val;
    if (0 == strncasecmp(&t[start],
			 _("minutes"),
			 strlen(_("minutes"))))
      ret += cronMINUTES * val;
    else if (0 == strncasecmp(&t[start],
			      _("seconds"),
			      strlen(_("seconds"))))
      ret += cronSECONDS * val;
    else if (0 == strncasecmp(&t[start],
			      _("hours"),
			      strlen(_("hours"))))
      ret += cronHOURS * val;
    else if (0 == strncasecmp(&t[start],
			      _("days"),
			      strlen(_("days"))))
      ret += cronDAYS  * val;
    else
      return SYSERR; /* parse error */
    while ( t[pos] == ' ')
      pos++;
  }
  *param = ret / cronSECONDS;
  return OK;
}

/**
 * FIXME: convert this into a glade widget!
 */
GtkWidget *
buildSearchTabLabel(GtkWidget *searchPage, 
		    const char *title) {
  GtkWidget *hbox;
  GtkWidget *label_hbox;
  GtkWidget *label_ebox;
  GtkWidget *label;
  GtkWidget *dummy_label;
  GtkWidget *close_button;
  GtkRcStyle *rcstyle;
  GtkRequisition size;
  GtkWidget *image;
  char *short_title;
  char *orig_title;
  char *final_title;
  char *short_title_end;
  char *tip_title;
  int short_title_len;
  static GtkTooltips *searchTabLabelTooltip = NULL;
  unsigned int *file_count;

  if(!searchTabLabelTooltip)
    searchTabLabelTooltip = gtk_tooltips_new();

  hbox = gtk_hbox_new (FALSE, 2);

  label_ebox = gtk_event_box_new ();
  gtk_event_box_set_visible_window (GTK_EVENT_BOX (label_ebox), FALSE);
  gtk_box_pack_start (GTK_BOX (hbox), label_ebox, TRUE, TRUE, 0);

  label_hbox = gtk_hbox_new (FALSE, 2);
  gtk_container_add (GTK_CONTAINER (label_ebox), label_hbox);

  /* setup close button */
  close_button = gtk_button_new ();
  gtk_button_set_relief (GTK_BUTTON (close_button),
			 GTK_RELIEF_NONE);
  /* don't allow focus on the close button */
  gtk_button_set_focus_on_click (GTK_BUTTON (close_button), FALSE);

  /* make it as small as possible */
  rcstyle = gtk_rc_style_new ();
  rcstyle->xthickness = 0;
  rcstyle->ythickness = 0;
  gtk_widget_modify_style (close_button, rcstyle);
  gtk_rc_style_unref (rcstyle),

  image = gtk_image_new_from_stock (GTK_STOCK_CLOSE,
		                    GTK_ICON_SIZE_MENU);
  gtk_widget_size_request (image, &size);
  gtk_widget_set_size_request (close_button, size.width, size.height);
  gtk_container_add (GTK_CONTAINER (close_button), image);
  gtk_box_pack_start (GTK_BOX (hbox), close_button, FALSE, FALSE, 0);

  g_signal_connect_swapped(close_button,
	                   "clicked",
	                   G_CALLBACK (on_closeSearchButton_clicked_fs),
		           searchPage);
	
  /* truncate the description if needed */
  if(g_utf8_strlen(title, 16) > 15) {
    short_title_end = g_utf8_offset_to_pointer (title, 15);
    short_title_len = short_title_end - title;
    short_title = g_strndup(title, short_title_len);
    orig_title = g_strconcat(short_title, "...", NULL);
    FREE(short_title); 
  } else {
    orig_title = STRDUP(title);
  }

  /* setup label */
  final_title = g_strconcat(orig_title, " (0)", NULL);
  label = gtk_label_new (final_title);
  FREE(final_title);

  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_misc_set_padding (GTK_MISC (label), 0, 0);
  gtk_box_pack_start (GTK_BOX (label_hbox), label, FALSE, FALSE, 0);

  /* add a forced space before the button */
  dummy_label = gtk_label_new ("");
  gtk_box_pack_start (GTK_BOX (label_hbox), dummy_label, TRUE, TRUE, 0);

  /* tooltips */
  gtk_tooltips_set_tip(searchTabLabelTooltip, close_button,
		       _("Close this search"), NULL);
  tip_title = g_strconcat (_("Search: "), title, NULL);
  gtk_tooltips_set_tip(searchTabLabelTooltip, label_ebox,
                             tip_title, NULL);

  /* store some references to access count & title later */
  file_count = malloc(sizeof(unsigned int));
  *file_count = 0;
  g_object_set_data(G_OBJECT(searchPage),
                    "file_count", (gpointer) file_count);
  g_object_set_data(G_OBJECT(searchPage),
                    "label", (gpointer) label);
  g_object_set_data(G_OBJECT(searchPage),
                    "title", (gpointer) orig_title);
	
  gtk_widget_show (hbox);
  gtk_widget_show (label_ebox);
  gtk_widget_show (label_hbox);
  gtk_widget_show (label);
  gtk_widget_show (dummy_label);	
  gtk_widget_show (image);
  gtk_widget_show (close_button);

  return hbox;
}