00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00026
00027
00028
00029
00030
00031 #include <stdio.h>
00032 #include <glib.h>
00033 #include <glib/gstdio.h>
00034 #include "browserTypes.h"
00035 #include "selection.h"
00036 #include "selectionFileHandler.h"
00037 #include "browserLog.h"
00038
00039 #ifdef SELECTION_ENABLED
00040
00041
00042 static int selection_file_get_name(GtkMozEmbed * b, char *name, unsigned int length);
00043
00044
00045
00046 bool selection_file_destroy(GtkMozEmbed * b)
00047 {
00048 char filename[MAX_FILE_NAME];
00049
00050 if (selection_file_get_name(GTK_MOZ_EMBED(b), filename, MAX_FILE_NAME) == 0)
00051 {
00052 BR_LOGPRINTF("name %s", filename);
00053
00054 if (g_file_test(filename, G_FILE_TEST_EXISTS))
00055 {
00056
00057 if (remove(filename) == 0)
00058 {
00059 return TRUE;
00060 }
00061 else
00062 {
00063 BR_ERRORPRINTF("remove(%s) != 0", filename);
00064 }
00065 }
00066 }
00067 return FALSE;
00068 }
00069
00070
00071 bool selection_file_available(GtkMozEmbed * b)
00072 {
00073 char filename[MAX_FILE_NAME];
00074
00075 if (selection_file_get_name(GTK_MOZ_EMBED(b), filename, MAX_FILE_NAME) == 0)
00076 {
00077 return g_file_test(filename, G_FILE_TEST_EXISTS);
00078 }
00079 return FALSE;
00080 }
00081
00082
00083
00084
00085 static int selection_file_get_name(GtkMozEmbed * b, char *name, unsigned int length)
00086 {
00087 char *url = NULL;
00088 char *ptr_1 = NULL;
00089 char *ptr_2 = NULL;
00090 char filename[MAX_FILE_NAME];
00091
00092
00093 url = gtk_moz_embed_get_location(GTK_MOZ_EMBED(b));
00094
00095 if (url)
00096 {
00097 BR_LOGPRINTF("url %s", url);
00098
00099 ptr_1 = strchr(url, ':');
00100 ptr_2 = strrchr(url, '.');
00101
00102
00103
00104
00105
00106 if (ptr_1 && ptr_2 && ((ptr_2 - ptr_1) < MAX_FILE_NAME - strlen("sel")))
00107 {
00108 ptr_1 = ptr_1 + 3;
00109 strcpy(filename, ptr_1);
00110 ptr_2 = strrchr(filename, '.');
00111 strcpy(++ptr_2, "sel");
00112 BR_LOGPRINTF("file name %s", filename);
00113
00114 if (strlen(filename) < length)
00115 {
00116 strcpy(name, filename);
00117 BR_LOGPRINTF("returning %s", name);
00118 return 0;
00119 }
00120 }
00121 else
00122 {
00123 BR_ERRORPRINTF("file name problem");
00124 }
00125
00126 g_free(url);
00127 }
00128 return 1;
00129 }
00130
00131
00132 FILE *selection_file_init(GtkMozEmbed * b)
00133 {
00134 char filename[MAX_FILE_NAME];
00135 FILE *selectionFile;
00136
00137 if (selection_file_get_name(GTK_MOZ_EMBED(b), filename, MAX_FILE_NAME) == 0)
00138 {
00139 BR_LOGPRINTF("file %s", filename);
00140
00141
00142 selectionFile = fopen(filename, "a+");
00143
00144 if (selectionFile)
00145 {
00146 BR_LOGPRINTF("opened file 0x%x", (unsigned int) selectionFile);
00147 return selectionFile;
00148 }
00149 else
00150 {
00151 BR_ERRORPRINTF("can not open/create selection file %s", filename);
00152 }
00153 }
00154 return NULL;
00155 }
00156
00157
00158 bool selection_file_store(FILE * selectionFile, SelectionID * theSelection)
00159 {
00160 if (selectionFile && theSelection)
00161 {
00162 fprintf(selectionFile, "selection\n");
00163 fprintf(selectionFile, "parentNodeTagName= %s\n", theSelection->parentNodeTagName);
00164 fprintf(selectionFile, "parentNodeIndex= %d\n", theSelection->parentNodeIndex);
00165 fprintf(selectionFile, "nodeIndex= %d\n", theSelection->nodeIndex);
00166 fprintf(selectionFile, "selectionStart= %d\n", theSelection->selectionStart);
00167 fprintf(selectionFile, "selectionLength= %d\n", theSelection->selectionLength);
00168
00169 BR_LOGPRINTF("selection");
00170 BR_LOGPRINTF("parentNodeTagName= %s", theSelection->parentNodeTagName);
00171 BR_LOGPRINTF("parentNodeIndex= %d", theSelection->parentNodeIndex);
00172 BR_LOGPRINTF("nodeIndex= %d", theSelection->nodeIndex);
00173 BR_LOGPRINTF("selectionStart= %d", theSelection->selectionStart);
00174 BR_LOGPRINTF("selectionLength= %d", theSelection->selectionLength);
00175 return TRUE;
00176 }
00177 else
00178 {
00179 BR_ERRORPRINTF("input parameter error");
00180 return FALSE;
00181 }
00182 }
00183
00184
00185 bool selection_file_close(FILE * selectionFile)
00186 {
00187 if (selectionFile != NULL)
00188 {
00189 BR_LOGPRINTF("Close the currently used selection file 0x%x", (unsigned int) selectionFile);
00190 fclose(selectionFile);
00191 selectionFile = NULL;
00192 return TRUE;
00193 }
00194 return FALSE;
00195 }
00196
00197
00198
00199
00200
00201
00202 GSList *selection_file_get_list(FILE * selectionFile)
00203 {
00204 GSList *theSelectionList = NULL;
00205 SelectionID *theSelection = NULL;
00206
00207 if (selectionFile)
00208 {
00209
00210 rewind(selectionFile);
00211
00212 while (!feof(selectionFile))
00213 {
00214 theSelection = g_new0(SelectionID, 1);
00215 fscanf(selectionFile, "selection\n");
00216 fscanf(selectionFile, "parentNodeTagName= %s\n", &theSelection->parentNodeTagName);
00217 fscanf(selectionFile, "parentNodeIndex= %d\n", &theSelection->parentNodeIndex);
00218 fscanf(selectionFile, "nodeIndex= %d\n", &theSelection->nodeIndex);
00219 fscanf(selectionFile, "selectionStart= %d\n", &theSelection->selectionStart);
00220 fscanf(selectionFile, "selectionLength= %d\n", &theSelection->selectionLength);
00221
00222 BR_LOGPRINTF("\n------------------selection---------------------");
00223 BR_LOGPRINTF("parentNodeTagName= %s", theSelection->parentNodeTagName);
00224 BR_LOGPRINTF("parentNodeIndex= %d", theSelection->parentNodeIndex);
00225 BR_LOGPRINTF("nodeIndex= %d", theSelection->nodeIndex);
00226 BR_LOGPRINTF("selectionStart= %d", theSelection->selectionStart);
00227 BR_LOGPRINTF("selectionLength= %d", theSelection->selectionLength);
00228 BR_LOGPRINTF("---------------------------------------------------");
00229
00230 theSelectionList = g_slist_append(theSelectionList, theSelection);
00231 }
00232 }
00233 return theSelectionList;
00234 }
00235
00236 #endif //SELECTION_ENABLED