Back to index

lightning-sunbird  0.9+nobinonly
history.h
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Christopher Blizzard. Portions created by Christopher Blizzard are Copyright (C) Christopher Blizzard.  All Rights Reserved.
00018  * Portions created by the Initial Developer are Copyright (C) 2001
00019  * the Initial Developer. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Christopher Blizzard <blizzard@mozilla.org>
00023  *   Doug Turner <dougt@meer.net>  Branched from TestGtkEmbed.cpp
00024  *
00025  *   The 10LE Team (in alphabetical order)
00026  *   -------------------------------------
00027  *
00028  *    Ilias Biris       <ext-ilias.biris@indt.org.br> - Coordinator
00029  *    Afonso Costa      <afonso.costa@indt.org.br>
00030  *    Antonio Gomes     <antonio.gomes@indt.org.br>
00031  *    Diego Gonzalez    <diego.gonzalez@indt.org.br>
00032  *    Raoni Novellino   <raoni.novellino@indt.org.br>
00033  *    Andre Pedralho    <andre.pedralho@indt.org.br>
00034  *
00035  *
00036  * Alternatively, the contents of this file may be used under the terms of
00037  * either the GNU General Public License Version 2 or later (the "GPL"), or
00038  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00039  * in which case the provisions of the GPL or the LGPL are applicable instead
00040  * of those above. If you wish to allow use of your version of this file only
00041  * under the terms of either the GPL or the LGPL, and not to allow others to
00042  * use your version of this file under the terms of the MPL, indicate your
00043  * decision by deleting the provisions above and replace them with the notice
00044  * and other provisions required by the GPL or the LGPL. If you do not delete
00045  * the provisions above, a recipient may use your version of this file under
00046  * the terms of any one of the MPL, the GPL or the LGPL.
00047  *
00048  * ***** END LICENSE BLOCK ***** */
00049 
00050 #include "minimo_types.h"
00051 
00052 #include "preference.h"
00053 
00054 #include "gtkmozembed.h"
00055 
00056 /* History callbacks */
00057 void view_history(GtkWidget *embed);
00058 static void clear_history(GtkWidget *b, HistoryWindow *hwin);
00059 static void close_history_win(GtkWidget *button, HistoryWindow *hwin);
00060 static void on_history_clist_button_go_cb(GtkWidget *button, HistoryWindow *hwin);
00061 static void remove_from_history(GtkWidget *button, HistoryWindow *hwin);
00062 static void remove_from_list(gchar *text);
00063 static void on_history_destroy(GtkWidget *window);
00064 static void on_search_button_cb(GtkWidget *button, HistoryWindow *hwin);
00065 
00066 /* History functions*/
00067 void add_to_history(const gchar *url);
00068 void write_history(void);
00069 void read_history(void);
00070 
00071 /* Add an URL into History file */
00072 void add_to_history(const gchar *url) {
00073   GSList *list;
00074   gint equals = 0;
00075   
00076   read_history();
00077   
00078   if (!url || url == NULL) return ;
00079   if (!g_strncasecmp(url,"about:",6) || !g_strcasecmp(url,"")) return;
00080   
00081   while (prot == 1) {
00082   }    
00083   
00084   prot = 1;
00085   
00086   for (list = history; list ; list = list->next) {
00087     if (g_strcasecmp((const gchar*)list->data,url) == 0){
00088       equals = 1;
00089       prot = 0;
00090     }
00091   }
00092   if (equals == 0) {
00093     history = g_slist_prepend(history,g_strdup(url));
00094     write_history();
00095   }
00096   prot = 0;
00097   
00098   
00099 }
00100 
00101 /* Create and show the History Window*/
00102 void view_history(GtkWidget *embed) {
00103   
00104   HistoryWindow *hwin;
00105   GSList *list;
00106   GtkWidget *image;
00107   
00108   hwin = g_new0(HistoryWindow,1);
00109   
00110   read_history();
00111   
00112   hwin->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00113   g_signal_connect(G_OBJECT(hwin->window),"destroy", G_CALLBACK(on_history_destroy), NULL);
00114   hwin->title = g_strdup_printf(("Minimo - History (%d) items"), g_slist_length(history));
00115   gtk_window_set_title(GTK_WINDOW(hwin->window),hwin->title);
00116   gtk_widget_set_usize(hwin->window,230,300);
00117   gtk_window_set_resizable(GTK_WINDOW(hwin->window),FALSE);
00118   gtk_window_set_position (GTK_WINDOW(hwin->window),GTK_WIN_POS_CENTER_ON_PARENT);
00119   gtk_window_set_modal (GTK_WINDOW (hwin->window), TRUE);
00120   gtk_window_set_keep_above(GTK_WINDOW (hwin->window), TRUE);
00121   g_free(hwin->title);
00122   
00123   hwin->vbox = gtk_vbox_new(FALSE,5);
00124   gtk_container_add(GTK_CONTAINER(hwin->window),hwin->vbox);
00125   
00126   hwin->scrolled_window = gtk_scrolled_window_new(NULL,NULL);
00127   gtk_box_pack_start(GTK_BOX(hwin->vbox),hwin->scrolled_window,TRUE,TRUE,0);
00128   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(hwin->scrolled_window),GTK_POLICY_NEVER,GTK_POLICY_AUTOMATIC);
00129   
00130   hwin->clist = gtk_clist_new(1);
00131   
00132   gtk_container_add(GTK_CONTAINER(hwin->scrolled_window),hwin->clist);
00133   
00134   hwin->search_label = gtk_label_new("Search: ");
00135   hwin->search_entry = gtk_entry_new();
00136   hwin->search_box = gtk_hbox_new(0,0);
00137   image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_SMALL_TOOLBAR);
00138   hwin->search_button = gtk_button_new();
00139   gtk_container_add(GTK_CONTAINER(hwin->search_button),image);
00140   gtk_tooltips_set_tip (gtk_tooltips_new (), GTK_WIDGET (hwin->search_button), "Find on history", NULL);
00141   image = gtk_image_new_from_stock (GTK_STOCK_YES, GTK_ICON_SIZE_SMALL_TOOLBAR);
00142   hwin->go_button = gtk_button_new ();
00143   gtk_container_add(GTK_CONTAINER(hwin->go_button),image);
00144   gtk_tooltips_set_tip (gtk_tooltips_new (), GTK_WIDGET (hwin->go_button), "Go to", NULL);
00145   
00146   hwin->embed = embed;
00147   
00148   g_signal_connect(G_OBJECT(hwin->search_button), "clicked", G_CALLBACK(on_search_button_cb), hwin);
00149   g_signal_connect(G_OBJECT(hwin->go_button), "clicked", G_CALLBACK(on_history_clist_button_go_cb), hwin);
00150   
00151   gtk_box_pack_start(GTK_BOX(hwin->search_box), hwin->search_label, FALSE, FALSE, 0);
00152   gtk_box_pack_start(GTK_BOX(hwin->search_box), hwin->search_entry, TRUE, TRUE, 0);
00153   gtk_box_pack_start(GTK_BOX(hwin->search_box), hwin->search_button, FALSE, FALSE, 0);
00154   gtk_box_pack_start(GTK_BOX(hwin->search_box), hwin->go_button, FALSE, FALSE, 0);
00155   
00156   hwin->remove = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
00157   g_signal_connect(G_OBJECT(hwin->remove), "clicked", G_CALLBACK(remove_from_history), hwin);
00158   
00159   hwin->clear = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
00160   g_signal_connect(G_OBJECT(hwin->clear), "clicked", G_CALLBACK(clear_history), hwin);
00161   
00162   hwin->close = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
00163   g_signal_connect(G_OBJECT(hwin->close), "clicked", G_CALLBACK(close_history_win), hwin);
00164   
00165   hwin->btnbox = gtk_hbox_new(0,0);
00166   gtk_box_pack_start(GTK_BOX(hwin->vbox), hwin->search_box, FALSE, FALSE, 0);
00167   gtk_box_pack_start(GTK_BOX(hwin->vbox), hwin->btnbox, FALSE, FALSE, 0);
00168   gtk_box_pack_start(GTK_BOX(hwin->btnbox), hwin->remove, TRUE, TRUE, 0);
00169   gtk_box_pack_start(GTK_BOX(hwin->btnbox), hwin->clear, TRUE, TRUE, 0);
00170   gtk_box_pack_start(GTK_BOX(hwin->btnbox), hwin->close, TRUE, TRUE, 0);
00171   
00172   
00173   while (prot == 1) {
00174   }    
00175   
00176   prot = 1;
00177   for (list = history; list ; list = list->next) {
00178     gchar *clist_entry[1];
00179     clist_entry[0] = (gchar *)list->data;
00180     gtk_clist_append(GTK_CLIST(hwin->clist),clist_entry);
00181   }
00182   prot = 0;
00183   gtk_widget_show_all(hwin->window);
00184 }
00185 
00186 /* Go to URL selected in the history list */
00187 static void on_history_clist_button_go_cb(GtkWidget *button, HistoryWindow *hwin) {
00188   GList *selection;
00189   gchar *location;
00190   gint row;
00191   
00192   selection = GTK_CLIST(hwin->clist)->selection;
00193   
00194   if (g_list_length(selection) == 0)  return;
00195   
00196   row = (gint)g_list_nth_data(selection,0);
00197   gtk_clist_get_text(GTK_CLIST(hwin->clist),row,0,&location);
00198   gtk_moz_embed_stop_load(GTK_MOZ_EMBED(hwin->embed));
00199   gtk_moz_embed_load_url(GTK_MOZ_EMBED(hwin->embed), location);
00200 }
00201 
00202 /* Close history window */
00203 static void close_history_win(GtkWidget *button, HistoryWindow *hwin) {
00204 
00205   gtk_widget_destroy(hwin->window);
00206 }
00207 
00208 /* Remove an URL from history list */
00209 static void remove_from_history(GtkWidget *button, HistoryWindow *hwin) {
00210   GList *selection = GTK_CLIST(hwin->clist)->selection;
00211   gchar *text;
00212   gint row;
00213   
00214   
00215   if (selection == NULL) return;
00216   
00217   gtk_clist_freeze(GTK_CLIST(hwin->clist));
00218   row = (gint) g_list_nth_data(selection,0);
00219   gtk_clist_get_text(GTK_CLIST(hwin->clist), row, 0, &text);
00220   remove_from_list(text);
00221   gtk_clist_remove(GTK_CLIST(hwin->clist), row); 
00222   gtk_clist_thaw(GTK_CLIST(hwin->clist));
00223   hwin->title = g_strdup_printf(("Minimo - History (%d) items"), g_slist_length(history));
00224   gtk_window_set_title(GTK_WINDOW(hwin->window),hwin->title);
00225   
00226 }
00227 
00228 /* Remove an URL from history file */
00229 static void remove_from_list(gchar *text) {
00230   GSList *l;
00231   
00232   for (l = history; l ; l = l->next) {
00233     gchar *data = (gchar *)l->data;
00234     if (!strcmp(text,data)) {
00235       history = g_slist_remove(history,data);
00236       return;
00237     }
00238   }
00239   
00240 }
00241 
00242 /* Remove all URL from history list */
00243 static void clear_history(GtkWidget *b, HistoryWindow *hwin) {
00244   gtk_clist_clear(GTK_CLIST(hwin->clist));
00245   g_slist_free(history);
00246   history = NULL;
00247   gtk_window_set_title(GTK_WINDOW(hwin->window),"Minimo - History (0) items");
00248 }
00249 
00250 /* Search an URL in the history list */
00251 static void on_search_button_cb(GtkWidget *button, HistoryWindow *hwin) {
00252   gint rows, i;
00253   G_CONST_RETURN gchar *search_text;
00254   gint search_pos = 0;
00255   
00256   rows = GTK_CLIST(hwin->clist)->rows;
00257   if (!rows) return;
00258   search_text = gtk_entry_get_text(GTK_ENTRY(hwin->search_entry));
00259   if (!search_text || !strcmp(search_text,"")) return; 
00260   if (search_pos >= rows) search_pos = 0;
00261   if (search_pos) search_pos++;
00262   for (i = search_pos; i < rows ; i++) {
00263     gchar *tmp, *tmp2 = NULL;
00264     gtk_clist_get_text(GTK_CLIST(hwin->clist), i, 0, &tmp);
00265     g_return_if_fail(tmp != NULL);
00266     tmp2 = strstr(tmp,search_text);
00267     if (tmp2 != NULL) {
00268       gtk_clist_moveto(GTK_CLIST(hwin->clist), i, 0, 0.0, 0.0);
00269       gtk_clist_select_row(GTK_CLIST(hwin->clist), i, 0);
00270       GTK_CLIST(hwin->clist)->focus_row = i;
00271       search_pos = i;
00272     }                              
00273     
00274   }
00275 }
00276 
00277 /* Close history window */
00278 static void on_history_destroy(GtkWidget *window) {
00279   write_history();
00280 }
00281 
00282 /* Read the history file */
00283 void read_history(void) {
00284   gchar *line;
00285   gchar *url;
00286   gchar *user_home = NULL;
00287   FILE   *g_history_file = NULL;
00288   gchar  *g_history_file_name = NULL;
00289   
00290   history = NULL;
00291   
00292   user_home = g_strconcat(g_get_home_dir(),NULL);
00293   g_history_file_name = g_strconcat(user_home,"/.Minimo/history",NULL);
00294   
00295   if ((g_history_file = fopen(g_history_file_name,"r"))!= NULL)
00296   {
00297     fclose(g_history_file);
00298     g_history_file = fopen(g_history_file_name,"r+");
00299   }
00300   else
00301     g_history_file = fopen(g_history_file_name,"w+");
00302   
00303   if (g_history_file == NULL) {
00304     printf("No History file to read!\n");
00305   }
00306   line = (gchar *)g_malloc(1024);
00307   
00308   while(fgets(line,1024,g_history_file) != NULL) {
00309     line[strlen(line)-1] = '\0';
00310     url = g_strdup(line);
00311     history = g_slist_append(history,url);
00312   }
00313   
00314   fclose(g_history_file);
00315   g_free(line);
00316   
00317 }
00318 
00319 /* Write the history file */
00320 void write_history(void) {
00321   GSList *list;
00322   gchar *user_home= NULL;
00323   FILE   *g_history_file = NULL;
00324   gchar  *g_history_file_name = NULL;
00325   
00326   user_home = g_strconcat(g_get_home_dir(),NULL);
00327   g_history_file_name = g_strconcat(user_home,"/.Minimo/history",NULL);
00328   
00329   g_history_file = fopen(g_history_file_name,"w");
00330   if (g_history_file == NULL) {
00331     printf("Couldn't open history file for writing!\n");
00332     return;
00333   }
00334   
00335   for (list = history; list ; list = list->next) {
00336     fprintf(g_history_file,"%s\n",(gchar *)(list->data));
00337   }
00338   
00339   fflush(g_history_file);     
00340   fclose(g_history_file);
00341   g_free(g_history_file_name);
00342   
00343 }