summaryrefslogtreecommitdiff
path: root/src/file-manager/nautilus-indexing-info.c
blob: 85e445e121dda74dc10051e432a80a3ad221166d (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */

/*
 *  nautilus-indexing-info.c: Indexing Info for the search service
 *
 *  Copyright (C) 2000 Eazel, Inc.
 *
 *  This library 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 of the License, or (at your option) any later version.
 *
 *  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
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public
 *  License along with this library; if not, write to the Free
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Authors: George Lebl <jirka@5z.com>
 *           Rebecca Schulman <rebecka@eazel.com>
 */

#include <config.h>
#include "nautilus-indexing-info.h"

#include <gtk/gtkhbox.h>
#include <gtk/gtkmain.h>
#include <gtk/gtkprogressbar.h>
#include <gtk/gtkvbox.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-uidefs.h>
#include <eel/eel-debug.h>
#include <eel/eel-gdk-extensions.h>
#include <eel/eel-glib-extensions.h>
#include <eel/eel-gtk-extensions.h>
#include <libnautilus-private/nautilus-medusa-support.h>
#include <eel/eel-stock-dialogs.h>

#ifdef HAVE_MEDUSA

#include <libmedusa/medusa-index-service.h>
#include <libmedusa/medusa-index-progress.h>
#include <libmedusa/medusa-indexed-search.h>
#include <libmedusa/medusa-system-state.h>

#define PROGRESS_UPDATE_INTERVAL 5000

typedef struct {
        GtkLabel *progress_label;
        GtkProgressBar *progress_bar;
} ProgressChangeData;

typedef struct {
        GtkDialog *last_index_time_dialog;
        GtkDialog *index_in_progress_dialog;
        gboolean indexing_is_in_progress;
} IndexingInfoDialogs;

static IndexingInfoDialogs *dialogs = NULL;

static GtkDialog *   last_index_time_dialog_new      (void);

static int
get_index_percentage_complete (void)
{
        return medusa_index_progress_get_percentage_complete ();
}

/* We set up a callback so that if medusa state changes, we
   close the dialog.  To avoid race conditions with removing
   the callback entirely when we destroy it, we set it so
   that the dialog just hides, rather than closing */
static void
set_close_hides_for_dialog (GtkDialog *dialog)
{
#if GNOME2_CONVERSION_COMPLETE
        gtk_dialog_set_close (dialog, TRUE /*click_closes*/);
        gtk_dialog_close_hides (dialog, TRUE /*just_hide*/);
#endif

}

static char *
get_text_for_progress_label (void)
{
        return g_strdup_printf (_("Indexing is %d%% complete."), get_index_percentage_complete ());
}


static gboolean
update_progress_display (gpointer callback_data)
{
        GtkLabel *progress_label;
        ProgressChangeData *progress_change_data;
        char *progress_string;

        progress_change_data = (ProgressChangeData *) callback_data;

        progress_label = GTK_LABEL (progress_change_data->progress_label);
        progress_string = get_text_for_progress_label ();
        gtk_label_set_text (progress_label, progress_string);
        g_free (progress_string);
        gtk_progress_bar_set_fraction (progress_change_data->progress_bar, 
                                       get_index_percentage_complete () / 100.0f);


        return TRUE;
}

static void
dialog_close_cover (gpointer dialog_data)
{
        g_assert (GTK_IS_DIALOG (dialog_data));

#if GNOME2_CONVERSION_COMPLETE
        gtk_dialog_close (dialog_data);
#endif
}

static void
show_index_progress_dialog (void)
{
        int callback_id;
        
#if GNOME2_CONVERSION_COMPLETE
        gtk_dialog_close (dialogs->last_index_time_dialog);
#endif
        gtk_widget_show_all (GTK_WIDGET (dialogs->index_in_progress_dialog));
        callback_id = medusa_execute_once_when_system_state_changes (dialog_close_cover,
                                                                     dialogs->index_in_progress_dialog);
        g_signal_connect_swapped (dialogs->index_in_progress_dialog, "destroy",
                                  G_CALLBACK (medusa_remove_state_changed_function),
                                  GINT_TO_POINTER (callback_id));
}


static void
show_last_index_time_dialog (void)
{
        int callback_id;

#if GNOME2_CONVERSION_COMPLETE
        gtk_dialog_close (dialogs->index_in_progress_dialog);
#endif
        gtk_widget_show_all (GTK_WIDGET (dialogs->last_index_time_dialog));
        callback_id = medusa_execute_once_when_system_state_changes (dialog_close_cover,
                                                                     dialogs->last_index_time_dialog);
        g_signal_connect_swapped (dialogs->last_index_time_dialog, "destroy",
                                  G_CALLBACK (medusa_remove_state_changed_function),
                                  GINT_TO_POINTER (callback_id));
}

static GtkDialog *
last_index_time_dialog_new (void)
{
	char *time_str;
	char *label_str;
        GtkWidget *label;
        GtkDialog *dialog;

        time_str = nautilus_indexing_info_get_last_index_time ();
        label_str = g_strdup_printf (_("Your files were last indexed at %s."),
                                     time_str);
        g_free (time_str);
	
        dialog = eel_create_info_dialog (label_str,
	                                 _("Once a day your files and text content are indexed so "
                                           "your searches are fast. "),
                                         _("Indexing Status"),
                                         NULL);
        set_close_hides_for_dialog (dialog);

        return dialog;
}

static void
timeout_remove_callback (gpointer callback_data)
{
        gtk_timeout_remove (GPOINTER_TO_UINT (callback_data));
}

static GtkDialog *
index_progress_dialog_new (void)
{
        GtkWidget *progress_label;
        GtkWidget *indexing_progress_bar;
        GtkWidget *progress_bar_hbox, *embedded_vbox;
        GtkDialog *dialog;
        char *progress_string;
        int percentage_complete;
        ProgressChangeData *progress_data;
        guint timeout_id;
                
        dialog = eel_create_info_dialog (_("Your files are currently being indexed."),
                                         _("Once a day your files and text content are indexed so "
                                           "your searches are fast."), 
                                         _("Indexing Status"), NULL);
        set_close_hides_for_dialog (dialog);        
        percentage_complete = get_index_percentage_complete ();
        indexing_progress_bar = gtk_progress_bar_new ();

        embedded_vbox = gtk_vbox_new (FALSE, GNOME_PAD_SMALL);

        /* turn off text display by setting text to NULL */
        gtk_progress_bar_set_text (GTK_PROGRESS_BAR (indexing_progress_bar), NULL);
        /* Put the progress bar in an hbox to make it a more sane size */
        gtk_box_pack_start (GTK_BOX (embedded_vbox), indexing_progress_bar, FALSE, FALSE, 0);

        progress_string = get_text_for_progress_label ();
        progress_label = gtk_label_new (progress_string);
        g_free (progress_string);
        gtk_box_pack_start (GTK_BOX (embedded_vbox), progress_label, FALSE, FALSE, 0);

        progress_bar_hbox = gtk_hbox_new (FALSE, 0);
        gtk_box_pack_start (GTK_BOX (progress_bar_hbox), embedded_vbox,
                            FALSE, FALSE, GNOME_PAD_SMALL);

        gtk_box_pack_start (GTK_BOX (dialog->vbox), progress_bar_hbox,
                            FALSE, FALSE, 0);

        /* Keep the dialog current with actual indexing progress */
        progress_data = g_new (ProgressChangeData, 1);
        progress_data->progress_label = GTK_LABEL (progress_label);
        progress_data->progress_bar = GTK_PROGRESS_BAR (indexing_progress_bar);
        timeout_id = gtk_timeout_add_full (PROGRESS_UPDATE_INTERVAL,
                                           update_progress_display,
                                           NULL,
                                           progress_data,
                                           g_free);
        g_signal_connect (progress_bar_hbox, "destroy", /* FIXME: will be called twice */
                          G_CALLBACK (timeout_remove_callback),
                          GLONG_TO_POINTER (timeout_id));
        
        return dialog;
}

static void
destroy_indexing_info_dialogs_on_exit (void)
{
        gtk_widget_destroy (GTK_WIDGET (dialogs->index_in_progress_dialog));
        gtk_widget_destroy (GTK_WIDGET (dialogs->last_index_time_dialog));
        g_free (dialogs);
}

static void
show_indexing_info_dialog (void)
{
        GtkDialog *dialog_shown;
        char *details_string;
        int callback_id;

        if (!medusa_system_services_are_enabled ()) {
                details_string = nautilus_medusa_get_explanation_of_enabling ();
                dialog_shown = eel_show_info_dialog_with_details (_("There is no index of your files right now."),
                                                                    _("When Fast Search is enabled, Find creates an "
                                                                    "index to speed up searches.  Fast searching "
                                                                    "is not enabled on your computer, so you "
                                                                    "do not have an index right now."),
								    _("No Index of Files"),
                                                                    details_string,
                                                                  NULL);
                g_free (details_string);
                set_close_hides_for_dialog (dialog_shown);
                
                callback_id = medusa_execute_once_when_system_state_changes (dialog_close_cover,
                                                                             dialog_shown);
                g_signal_connect_swapped (dialog_shown, "destroy",
                                          G_CALLBACK (medusa_remove_state_changed_function),
                                          GINT_TO_POINTER (callback_id));
                return;
        }
	if (dialogs == NULL) {
                dialogs = g_new0 (IndexingInfoDialogs, 1);
                eel_debug_call_at_shutdown (destroy_indexing_info_dialogs_on_exit);
                
                dialogs->last_index_time_dialog = last_index_time_dialog_new ();
                dialogs->index_in_progress_dialog = index_progress_dialog_new ();
        }
 
       dialogs->indexing_is_in_progress = medusa_indexing_is_currently_in_progress ();
       
       if (dialogs->indexing_is_in_progress) {
               show_index_progress_dialog ();
       } else {
               show_last_index_time_dialog ();
       }
        
}

#endif /* HAVE_MEDUSA */

#ifndef HAVE_MEDUSA
static void
show_search_service_not_available_dialog (void)
{
        eel_show_error_dialog (_("Sorry, but the medusa search service is not available."),
                                    _("Please verify medusa has been setup correctly."),
                                    _("Search Service Not Available"),
                                    NULL);
}
#endif


char *
nautilus_indexing_info_get_last_index_time (void)
{
#ifdef HAVE_MEDUSA
	time_t update_time;
        
	update_time = medusa_index_service_get_last_index_update_time ();
        if (update_time) {
                return eel_strdup_strftime (_("%I:%M %p, %x"),
                                                 localtime (&update_time));
        } else {
                return NULL;
        }
#else
        g_warning ("called nautilus_indexing_info_get_last_index_time with HAVE_MEDUSA off");
        return g_strdup ("");
#endif
}

/**
 * nautilus_indexing_info_show_dialog:
 *
 * Show the indexing info dialog.  If one is already
 * running, just raise that one.
 **/
void
nautilus_indexing_info_show_dialog (void)
{
#ifdef HAVE_MEDUSA
        show_indexing_info_dialog ();
#else
        show_search_service_not_available_dialog ();
#endif
}