Back to index

lightning-sunbird  0.9+nobinonly
minimo_history.cpp
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_history.h"
00051 
00052 gint   gHistoryProt = 0;
00053 
00054 GSList        *gHistoryList = NULL;
00055 
00056 /* Add an URL into History file */
00057 void add_to_history(const gchar *url) {
00058   GSList *list;
00059   gint equals = 0;
00060   
00061   read_history();
00062   
00063   if (!url || url == NULL) return ;
00064   if (!g_strncasecmp(url,"about:",6) || !g_strcasecmp(url,"")) return;
00065   
00066   while (gHistoryProt == 1) {
00067   }    
00068   
00069   gHistoryProt = 1;
00070   
00071   for (list = gHistoryList; list ; list = list->next) {
00072     if (g_strcasecmp((const gchar*)list->data,url) == 0){
00073       equals = 1;
00074       gHistoryProt = 0;
00075     }
00076   }
00077   if (equals == 0) {
00078     gHistoryList = g_slist_prepend(gHistoryList,g_strdup(url));
00079     write_history();
00080   }
00081   gHistoryProt = 0;
00082   
00083   
00084 }
00085 
00086 /* Create and show the History Window*/
00087 void view_history(GtkWidget *embed) {
00088   
00089   HistoryWindow *hwin;
00090   GSList *list;
00091   GtkWidget *image;
00092   
00093   hwin = g_new0(HistoryWindow,1);
00094   
00095   read_history();
00096   
00097   //hwin->window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00098   hwin->window = gtk_dialog_new();
00099   gtk_window_set_position(GTK_WINDOW(hwin->window), GTK_WIN_POS_CENTER_ON_PARENT);
00100   gtk_window_set_title(GTK_WINDOW(hwin->window),hwin->title);
00101   gtk_widget_set_usize(hwin->window,230,300);
00102   gtk_window_set_resizable(GTK_WINDOW(hwin->window),FALSE);
00103   gtk_window_set_position (GTK_WINDOW(hwin->window),GTK_WIN_POS_CENTER_ON_PARENT);
00104   gtk_window_set_modal (GTK_WINDOW (hwin->window), TRUE);
00105   gtk_window_set_keep_above(GTK_WINDOW (hwin->window), TRUE);
00106   
00107   g_signal_connect(G_OBJECT(hwin->window),"destroy", G_CALLBACK(on_history_destroy), NULL);
00108   hwin->title = g_strdup_printf(("Minimo - History (%d) items"), g_slist_length(gHistoryList));
00109   g_free(hwin->title);
00110   
00111   hwin->vbox = gtk_vbox_new(FALSE,5);
00112   //   gtk_container_add(GTK_CONTAINER(hwin->window),hwin->vbox);
00113   
00114   gtk_box_pack_start(GTK_BOX(GTK_DIALOG(hwin->window)->vbox),hwin->vbox, TRUE, TRUE, 0);
00115   
00116   hwin->scrolled_window = gtk_scrolled_window_new(NULL,NULL);
00117   gtk_box_pack_start(GTK_BOX(hwin->vbox),hwin->scrolled_window,TRUE,TRUE,0);
00118   gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(hwin->scrolled_window),GTK_POLICY_NEVER,GTK_POLICY_AUTOMATIC);
00119   
00120   hwin->clist = gtk_clist_new(1);
00121   
00122   gtk_container_add(GTK_CONTAINER(hwin->scrolled_window),hwin->clist);
00123   
00124   hwin->search_label = gtk_label_new("Search: ");
00125   hwin->search_entry = gtk_entry_new();
00126   hwin->search_box = gtk_hbox_new(0,0);
00127   image = gtk_image_new_from_stock (GTK_STOCK_FIND, GTK_ICON_SIZE_SMALL_TOOLBAR);
00128   hwin->search_button = gtk_button_new();
00129   gtk_container_add(GTK_CONTAINER(hwin->search_button),image);
00130   gtk_tooltips_set_tip (gtk_tooltips_new (), GTK_WIDGET (hwin->search_button), "Find on history", NULL);
00131   image = gtk_image_new_from_stock (GTK_STOCK_YES, GTK_ICON_SIZE_SMALL_TOOLBAR);
00132   hwin->go_button = gtk_button_new ();
00133   gtk_container_add(GTK_CONTAINER(hwin->go_button),image);
00134   gtk_tooltips_set_tip (gtk_tooltips_new (), GTK_WIDGET (hwin->go_button), "Go to", NULL);
00135   
00136   hwin->embed = embed;
00137   
00138   g_signal_connect(G_OBJECT(hwin->search_button), "clicked", G_CALLBACK(on_search_button_cb), hwin);
00139   g_signal_connect(G_OBJECT(hwin->go_button), "clicked", G_CALLBACK(on_history_clist_button_go_cb), hwin);
00140   
00141   gtk_box_pack_start(GTK_BOX(hwin->search_box), hwin->search_label, FALSE, FALSE, 0);
00142   gtk_box_pack_start(GTK_BOX(hwin->search_box), hwin->search_entry, TRUE, TRUE, 0);
00143   gtk_box_pack_start(GTK_BOX(hwin->search_box), hwin->search_button, FALSE, FALSE, 0);
00144   gtk_box_pack_start(GTK_BOX(hwin->search_box), hwin->go_button, FALSE, FALSE, 0);
00145   
00146   hwin->remove = gtk_button_new_from_stock(GTK_STOCK_REMOVE);
00147   g_signal_connect(G_OBJECT(hwin->remove), "clicked", G_CALLBACK(remove_from_history), hwin);
00148   
00149   hwin->clear = gtk_button_new_from_stock(GTK_STOCK_CLEAR);
00150   g_signal_connect(G_OBJECT(hwin->clear), "clicked", G_CALLBACK(clear_history), hwin);
00151   
00152   hwin->close = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
00153   g_signal_connect(G_OBJECT(hwin->close), "clicked", G_CALLBACK(close_history_win), hwin);
00154   
00155   hwin->btnbox = gtk_hbox_new(0,0);
00156   gtk_box_pack_start(GTK_BOX(hwin->vbox), hwin->search_box, FALSE, FALSE, 0);
00157   gtk_box_pack_start(GTK_BOX(hwin->vbox), hwin->btnbox, FALSE, FALSE, 0);
00158   gtk_box_pack_start(GTK_BOX(hwin->btnbox), hwin->remove, TRUE, TRUE, 0);
00159   gtk_box_pack_start(GTK_BOX(hwin->btnbox), hwin->clear, TRUE, TRUE, 0);
00160   gtk_box_pack_start(GTK_BOX(hwin->btnbox), hwin->close, TRUE, TRUE, 0);
00161   
00162   
00163   while (gHistoryProt == 1) {
00164   }    
00165   
00166   gHistoryProt = 1;
00167   for (list = gHistoryList; list ; list = list->next) {
00168     gchar *clist_entry[1];
00169     clist_entry[0] = (gchar *)list->data;
00170     gtk_clist_append(GTK_CLIST(hwin->clist),clist_entry);
00171   }
00172   gHistoryProt = 0;
00173   gtk_widget_show_all(hwin->window);
00174 }
00175 
00176 /* Go to URL selected in the history list */
00177 void on_history_clist_button_go_cb(GtkWidget *button, HistoryWindow *hwin) {
00178   GList *selection;
00179   gchar *location;
00180   gint row;
00181   
00182   selection = GTK_CLIST(hwin->clist)->selection;
00183   
00184   if (g_list_length(selection) == 0)  return;
00185   
00186   row = (gint)g_list_nth_data(selection,0);
00187   gtk_clist_get_text(GTK_CLIST(hwin->clist),row,0,&location);
00188   gtk_moz_embed_stop_load(GTK_MOZ_EMBED(hwin->embed));
00189   gtk_moz_embed_load_url(GTK_MOZ_EMBED(hwin->embed), location);
00190 }
00191 
00192 /* Close history window */
00193 void close_history_win(GtkWidget *button, HistoryWindow *hwin)
00194 {
00195   gtk_widget_destroy(hwin->window);
00196 }
00197 
00198 /* Remove an URL from history list */
00199 void remove_from_history(GtkWidget *button, HistoryWindow *hwin)
00200 {
00201   GList *selection = GTK_CLIST(hwin->clist)->selection;
00202   gchar *text;
00203   gint row;
00204   
00205   
00206   if (selection == NULL) return;
00207   
00208   gtk_clist_freeze(GTK_CLIST(hwin->clist));
00209   row = (gint) g_list_nth_data(selection,0);
00210   gtk_clist_get_text(GTK_CLIST(hwin->clist), row, 0, &text);
00211   remove_from_list(text);
00212   gtk_clist_remove(GTK_CLIST(hwin->clist), row); 
00213   gtk_clist_thaw(GTK_CLIST(hwin->clist));
00214   hwin->title = g_strdup_printf(("Minimo - History (%d) items"), g_slist_length(gHistoryList));
00215   gtk_window_set_title(GTK_WINDOW(hwin->window),hwin->title);
00216   
00217 }
00218 
00219 /* Remove an URL from history file */
00220 void remove_from_list(gchar *text)
00221 {
00222   GSList *l;
00223   
00224   for (l = gHistoryList; l ; l = l->next) {
00225     gchar *data = (gchar *)l->data;
00226     if (!strcmp(text,data)) {
00227       gHistoryList = g_slist_remove(gHistoryList,data);
00228       return;
00229     }
00230   }
00231   
00232 }
00233 
00234 /* Remove all URL from history list */
00235 void clear_history(GtkWidget *b, HistoryWindow *hwin)
00236 {
00237   gtk_clist_clear(GTK_CLIST(hwin->clist));
00238   g_slist_free(gHistoryList);
00239   gHistoryList = NULL;
00240   gtk_window_set_title(GTK_WINDOW(hwin->window),"Minimo - History (0) items");
00241 }
00242 
00243 /* Search an URL in the history list */
00244 void on_search_button_cb(GtkWidget *button, HistoryWindow *hwin)
00245 {
00246   gint rows, i;
00247   G_CONST_RETURN gchar *search_text;
00248   gint search_pos = 0;
00249   
00250   rows = GTK_CLIST(hwin->clist)->rows;
00251   if (!rows) return;
00252   search_text = gtk_entry_get_text(GTK_ENTRY(hwin->search_entry));
00253   if (!search_text || !strcmp(search_text,"")) return; 
00254   if (search_pos >= rows) search_pos = 0;
00255   if (search_pos) search_pos++;
00256   for (i = search_pos; i < rows ; i++) {
00257     gchar *tmp, *tmp2 = NULL;
00258     gtk_clist_get_text(GTK_CLIST(hwin->clist), i, 0, &tmp);
00259     g_return_if_fail(tmp != NULL);
00260     tmp2 = strstr(tmp,search_text);
00261     if (tmp2 != NULL) {
00262       gtk_clist_moveto(GTK_CLIST(hwin->clist), i, 0, 0.0, 0.0);
00263       gtk_clist_select_row(GTK_CLIST(hwin->clist), i, 0);
00264       GTK_CLIST(hwin->clist)->focus_row = i;
00265       search_pos = i;
00266     }                              
00267     
00268   }
00269 }
00270 
00271 /* Close history window */
00272 void on_history_destroy(GtkWidget *window)
00273 {
00274   write_history();
00275 }
00276 
00277 /* Read the history file */
00278 void read_history(void) {
00279   gchar *line;
00280   gchar *url;
00281   gchar *user_home = NULL;
00282   FILE   *g_history_file = NULL;
00283   gchar  *g_history_file_name = NULL;
00284   
00285   gHistoryList = NULL;
00286   
00287   user_home = g_strconcat(g_get_home_dir(),NULL);
00288   g_history_file_name = g_strconcat(user_home,"/.Minimo/history",NULL);
00289   
00290   if ((g_history_file = fopen(g_history_file_name,"r"))!= NULL)
00291   {
00292     fclose(g_history_file);
00293     g_history_file = fopen(g_history_file_name,"r+");
00294   }
00295   else
00296     g_history_file = fopen(g_history_file_name,"w+");
00297   
00298   if (g_history_file == NULL) {
00299     printf("No History file to read!\n");
00300   }
00301   line = (gchar *)g_malloc(1024);
00302   
00303   while(fgets(line,1024,g_history_file) != NULL) {
00304     line[strlen(line)-1] = '\0';
00305     url = g_strdup(line);
00306     gHistoryList = g_slist_append(gHistoryList,url);
00307   }
00308   
00309   fclose(g_history_file);
00310   g_free(line);
00311   
00312 }
00313 
00314 /* Write the history file */
00315 void write_history(void) {
00316   GSList *list;
00317   gchar *user_home= NULL;
00318   FILE   *g_history_file = NULL;
00319   gchar  *g_history_file_name = NULL;
00320   
00321   user_home = g_strconcat(g_get_home_dir(),NULL);
00322   g_history_file_name = g_strconcat(user_home,"/.Minimo/history",NULL);
00323   
00324   g_history_file = fopen(g_history_file_name,"w");
00325   if (g_history_file == NULL) {
00326     printf("Couldn't open history file for writing!\n");
00327     return;
00328   }
00329   
00330   for (list = gHistoryList; list ; list = list->next) {
00331     fprintf(g_history_file,"%s\n",(gchar *)(list->data));
00332   }
00333   
00334   fflush(g_history_file);     
00335   fclose(g_history_file);
00336   g_free(g_history_file_name);
00337   
00338 }