1 /*
2     Copyright 2007, 2008, 2009, 2010 Geyer Klaus
3 
4     This file is part of Cat'sEyE.
5 
6     Cat'sEyE is free software: you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation, either version 3 of the License, or
9     (at your option) any later version.
10 
11     Cat'sEyE is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15 
16     You should have received a copy of the GNU General Public License
17     along with Cat'sEyE.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 
21 #include <string.h>
22 #include <gtk/gtk.h>
23 #include <stdlib.h>
24 #include <dirent.h>	//Verzeichnis Zeiger und DIR - Struktur
25 
26 
27 #include "globalDefinitions.h"
28 #include "helpers.h"
29 #include "mytrace.h"
30 #include "listitems.h"
31 #include "Lists.h"
32 
33 #include "allInclude.h"
34 
35 extern GtkTextBuffer *g_TextBuffer_CommandHistory;
36 
CursorToWait(GtkWidget * widget)37 void CursorToWait(GtkWidget *widget){
38 
39     //    GdkWindow *gdkwindow = GTK_WIDGET (win)->window;
40 
41 //gint x,y;
42 //GdkWindow *gdkwindow = gdk_window_at_pointer(&x,&y);
43 //GdkWindow *gdkwindow = gtk_widget_get_parent_window        (Object->ScrollWindow);  //(GtkWidget *)win
44 //GdkWindow *gdkwindow = gtk_widget_get_root_window        (VPanedForFileShelf);  //2.2
45 //gtk_widget_get_window           //2.14
46 
47 /*
48     GdkCursor* cursor;
49     cursor = gdk_cursor_new(GDK_WATCH); //GDK_WATCH     GDK_CLOCK
50     gdk_window_set_cursor(gdkwindow, cursor);    //set the cursor to clock while we are updating        gdk_get_default_root_window()
51     gdk_cursor_destroy(cursor);
52 */
53     //As long as changing the mouse cursor does not work, we change the text in the Notebooktab
54  //   gtk_label_set_text(GTK_LABEL(Object->label),"please wait");
55 
56 
57 
58     GdkWindow *gdkwindow = gtk_widget_get_parent_window        (widget);
59     if (gdkwindow != NULL){
60         GdkCursor* cursor;
61         cursor = gdk_cursor_new(GDK_WATCH); //GDK_WATCH     GDK_CLOCK
62         gdk_window_set_cursor(gdkwindow, cursor);    //set the cursor to clock while we are updating        gdk_get_default_root_window()
63         gdk_cursor_destroy(cursor);
64     }
65 }
66 
67 
CursorToNormal(GtkWidget * widget)68 void CursorToNormal(GtkWidget *widget){
69     GdkWindow *gdkwindow = gtk_widget_get_parent_window        (widget);
70     gdk_window_set_cursor(gdkwindow, NULL);
71 }
72 
73 
GetTimeMine(GTimeVal time)74 double GetTimeMine(GTimeVal time){
75     return (double)time.tv_sec + (double)time.tv_usec*0.000001;
76 }
77 
78 
ExchangeSpecialCharachterForXML(GString * gstrText)79 int ExchangeSpecialCharachterForXML (GString *gstrText){
80     //will echange all special characters like &, <, >, �, � .... to their XML companions
81     TRACEIT(10,"ExchangeSpecialCharachterForXML		start");
82     if (gstrText == NULL){
83         TRACEIT(10,"ExchangeSpecialCharachterForXML		parameter NULL");
84         return -1;
85     }
86     /*
87     &      &amp;
88     '      &apos;
89     <      &lt;
90     >      &gt;
91     "      &quot;
92 
93     �      &#196;
94     �      &#214;
95     �      &#220;
96     �      &#228
97     �      &#246;
98     �      &#252;
99     �      &#223;
100     */
101 
102     char *cppKnownSpecials[] = { "&amp;", "&apos;", "&lt;", "&gt;", "&quot;", "&#196;", "&#214;", "&#220;", "&#228", "&#246;", "&#252;", "&#223;"};
103     int iKnwonSpectials = 12;
104 
105     GString *gstrEnemy = g_string_sized_new (2);
106     GString *gstrNewString = g_string_sized_new (9);
107 
108     g_string_assign (gstrEnemy, "�");
109     g_string_assign (gstrNewString, "&#214;");
110     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
111 
112     g_string_assign (gstrEnemy, "�");
113     g_string_assign (gstrNewString, "&#220;");
114     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
115 
116     g_string_assign (gstrEnemy, "�");
117     g_string_assign (gstrNewString, "&#228;");
118     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
119 
120     g_string_assign (gstrEnemy, "�");
121     g_string_assign (gstrNewString, "&#246;");
122     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
123 
124     g_string_assign (gstrEnemy, "�");
125     g_string_assign (gstrNewString, "&#252;");
126     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
127 
128     g_string_assign (gstrEnemy, "�");
129     g_string_assign (gstrNewString, "&#223;");
130     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
131 
132     g_string_assign (gstrEnemy, "'");
133     g_string_assign (gstrNewString, "&apos;");
134     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
135 
136     g_string_assign (gstrEnemy, "<");
137     g_string_assign (gstrNewString, "&lt;");
138     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
139 
140     g_string_assign (gstrEnemy, ">");
141     g_string_assign (gstrNewString, "&gt;");
142     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
143 
144     g_string_assign (gstrEnemy, "\"");
145     g_string_assign (gstrNewString, "&quot;");
146     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
147 
148     g_string_assign (gstrEnemy, "�");
149     g_string_assign (gstrNewString, "&#196;");
150     exchangeNameWithValue(gstrText, gstrEnemy, gstrNewString);
151 
152     char *cpBuf = NULL;
153     char *cpBuf2 = NULL;
154     int iDoNotExchangeThis = 0;
155     int iLocation = 0;
156     int i;
157     cpBuf = strstr (gstrText->str, "&");
158     while (cpBuf) {
159         iDoNotExchangeThis = 0;
160         for (i=0; i<iKnwonSpectials; i++){
161             cpBuf2 = strstr (cpBuf, cppKnownSpecials[i]);
162             if (cpBuf2 == cpBuf){
163                 iDoNotExchangeThis = 1;
164                 break;
165             }
166         }
167         if (iDoNotExchangeThis == 0){
168             iLocation = cpBuf - gstrText->str;
169             g_string_insert (gstrText,  iLocation+1, "amp;");
170         }
171 
172         cpBuf = cpBuf + 1;
173         cpBuf = strstr (cpBuf, "&");
174     }
175 
176     g_string_free (gstrEnemy, TRUE);
177     g_string_free (gstrNewString, TRUE);
178 
179     TRACEIT(10,"ExchangeSpecialCharachterForXML		end");
180     return 0;
181 }
182 
checkItemIsLink(const char * ccpItem)183 int checkItemIsLink (const char *ccpItem){
184     //return: -1=error, 0=NoLink, 1=Link
185     TRACEIT(10,"checkItemIsLink		start");
186     if (NULL == ccpItem){
187         TRACEIT (2, "checkItemIsLink      parameter NULL");
188         return -1;
189     }
190     struct FileAttributs attribs;
191     GString *gstrName = g_string_new (ccpItem);
192     GString *gstrPath = g_string_new (ccpItem);
193     int iRet = 0;
194 
195     checkPathForLastCharOtherwise(gstrName);
196     getLastStringInPath(gstrName);
197     GetPathFromPathWithName(gstrPath, 1);
198 
199     fileAttributesGet( &attribs, gstrPath->str, gstrName->str);
200     if (attribs.gstrLinkTarget){
201         g_string_free(attribs.gstrLinkTarget, TRUE);
202         iRet = 1;
203     }
204 
205     g_string_free (gstrName, TRUE);
206     g_string_free (gstrPath, TRUE);
207 
208     TRACEIT(10,"checkItemIsLink		start");
209     return iRet;
210 }
211 
212 
manipulateCounter(int * iCounter,gboolean bCountUp,const char * ccpSource)213 int manipulateCounter (int *iCounter, gboolean bCountUp, const char *ccpSource){
214     //CountUp = TRUE: iCount++
215     //CountUp = FALSE: iCount--
216     //Counter can not drop under 0
217     TRACEIT(10,"manipulateCounter		start");
218     if (iCounter == NULL){
219         TRACEIT (2, "manipulateCounter      parameter NULL");
220         return -1;
221     }
222 
223     if (bCountUp){
224         (*iCounter)++;
225     }
226     else{
227         (*iCounter)--;
228     }
229 
230     if (*iCounter < 0){
231         *iCounter = 0;
232         //TRACEIT (2, "manipulateCounter    droped below 0 from:");
233         //if (ccpSource != NULL){
234         //    TRACEIT (2, ccpSource);
235         //}
236     }
237 
238     TRACEIT(10,"manipulateCounter		end");
239     return 0;
240 }
241 
242 
exchangeNameWithValue(GString * gstrText,GString * gstrEnemy,GString * NewString)243 int exchangeNameWithValue(GString *gstrText, GString *gstrEnemy, GString *NewString){
244 	//gstrText holds the String in wich gstrEnemy is searched for and gstrEnemy will be exchanged by NewString
245 	TRACEIT(10,"exchangeNameWithValue		start");
246 	char *cStart;
247 	int iPosition=0;
248 	int iSize;
249 	int firstTime = 1;
250 	do {
251 		cStart = strstr(gstrText->str,gstrEnemy->str);
252 		if (cStart == NULL && firstTime==0){
253 			break;
254 		}
255 		else if(firstTime == 1 && cStart==NULL){
256 		    TRACEIT(10,"exchangeNameWithValue		end 1");
257 			return 0;	//EnemyString not found !!! unused Variable?
258 		}
259 		firstTime = 0;
260 		iSize = strlen(gstrEnemy->str);
261 		iPosition = 0;
262 		while( (gstrText->str)+iPosition != cStart){
263 			iPosition++;
264 		}
265 		g_string_erase(gstrText,iPosition,iSize);
266 		g_string_insert(gstrText,iPosition,NewString->str);
267 	}while (cStart != NULL);
268 	TRACEIT(10,"exchangeNameWithValue		end");
269 	return 1;
270 }
271 
272 
getCorrectGFileTypeFromLocation(char * location,int * iType)273 GFile *getCorrectGFileTypeFromLocation(char *location, int *iType){
274     //decides if we have a Path, File or an uri stored in location
275     //and if iType is not NULL, writes the type to it.
276     //return: A GFile Object (UNREF WITH g_object_unref()) or NULL on error.
277     //
278     //iType:    -1 no idea, 0=locale file, 1=localePath, 2=uri
279     TRACEIT(10,"getCorrectGFileTypeFromLocation		start");
280     if (location == NULL){
281         TRACEIT(10,"getCorrectGFileTypeFromLocation		end, Parameter NULL");
282         return NULL;
283     }
284 
285     int iMostProperlyLocale=-1;
286     gboolean bTestRegular;
287     gboolean bTestDir;
288     gboolean bTestExists;
289     GFile *gfilePo=NULL;
290 
291     int iIThinkItIs=-1;
292 
293     if(strlen(location)>=1){
294         if (location[0] == '/'){
295             iMostProperlyLocale=1;
296         }
297         else{
298             iMostProperlyLocale=0;
299         }
300     }
301 
302     bTestExists = g_file_test (location, G_FILE_TEST_EXISTS);
303     bTestDir = g_file_test (location, G_FILE_TEST_IS_DIR);
304     bTestRegular = g_file_test (location, G_FILE_TEST_IS_REGULAR);
305 
306 
307     if (bTestDir && bTestExists){
308         iIThinkItIs=1;  //PATH
309         gfilePo = g_file_new_for_path(location);
310         if (iType)
311             *iType = 1;
312     }
313     else if (bTestRegular && bTestExists){
314         iIThinkItIs=0;  //file
315         gfilePo = g_file_new_for_path(location);
316         if (iType)
317             *iType = 0;
318     }
319     else if (iMostProperlyLocale == 0){
320         iIThinkItIs=2;  //uri
321         gfilePo = g_file_new_for_uri(location);
322         if (iType)
323             *iType = 2;
324     }
325     else{
326         if (iType)
327             *iType = -1;
328     }
329 
330     TRACEIT(10,"getCorrectGFileTypeFromLocation		end");
331     return gfilePo;
332 }
333 
334 
getRandomIntNumber(int iMin,int iMax)335 int getRandomIntNumber(int iMin, int iMax){
336 	//returns a random Number within iMin and iMax (including).
337 	//if iMin or iMax is -1, no borders are applied
338 	//return: -1 on error, randomNumber on success
339 	TRACEIT(10,"getRandomIntNumber		start");
340 	int randomNumber=-1;
341 
342 	if ((iMin!=-1 || iMax!=-1) && iMin >= iMax){
343 		TRACEIT(4,"getRandomIntNumber		end, iMin>=iMax");
344 		return -1;
345 	}
346 
347 	//srand(time(NULL));
348 	if (iMin == -1 || iMax == -1){
349 		randomNumber = rand();
350 	}
351 	else{
352 		randomNumber = iMin + (int) ((iMax) * (rand()/(RAND_MAX+1.0)));
353 	}
354 
355 	TRACEIT(10,"getRandomIntNumber		end");
356 	return randomNumber;
357 }
358 
359 
BytesToHumanity(guint64 bytes,double * newBytes,int * iEPow,GString * gstrEPow)360 void BytesToHumanity(guint64 bytes, double *newBytes, int *iEPow, GString *gstrEPow){
361     //gets an amount of bytes and breaks them trough 1024 till its easy to read even for humans
362     //iEPow will get the potential by ten by which the new bytes correspont (can be NULL)
363     //gstrEPor will hold a string wich represents iEPow (can be NULL)
364     // example:    4 512 376 344 bytes ->  newBytes = 4.512 476 344, iEPow=3, gstrEPow = "GiB"
365     //
366     TRACEIT(10,"BytesToHumanity		start");
367 
368     if (newBytes == NULL){
369         TRACEIT(2,"BytesToHumanity		end, Parameter NULL");
370         return;
371     }
372     int i=0;
373     double dbuf = (double) bytes;
374     //while ((dbuf / 1024 > 1000.0)){
375     while ((dbuf > 1000.0)){
376         dbuf /= 1024;
377         i++;
378     }
379     //dbuf /= 1024;
380     //i++;
381     *newBytes = dbuf;
382     if (iEPow){
383         *iEPow=i;
384     }
385     if (gstrEPow){
386         switch (i){
387             case 0:
388                 g_string_assign(gstrEPow, "Byte");
389                 break;
390             case 1:
391                 g_string_assign(gstrEPow, "KiB");
392                 break;
393             case 2:
394                 g_string_assign(gstrEPow, "MiB");
395                 break;
396             case 3:
397                 g_string_assign(gstrEPow, "GiB");
398                 break;
399             case 4:
400                 g_string_assign(gstrEPow, "TiB");
401                 break;
402         }
403     }
404 
405     TRACEIT(10,"BytesToHumanity		end");
406 }
407 
408 
BytesToHumanityString(guint64 bytes,GString * gstrBytesString,int decimalplaces)409 void BytesToHumanityString (guint64 bytes, GString *gstrBytesString, int decimalplaces){
410     //converts the bytes to a format humans can read easely
411     //4567 -> "4.567 KiB"
412     TRACEIT(10,"BytesToHumanityString		start");
413     if (gstrBytesString == NULL){
414         TRACEIT(2,"BytesToHumanityString		end, Parameter NULL");
415     }
416     double newBytes;
417     GString *gstrEPow = g_string_sized_new(10);
418 
419     BytesToHumanity(bytes,&newBytes,NULL, gstrEPow);
420     g_string_printf(gstrBytesString, "%0.2f %s", newBytes, gstrEPow->str);
421     //free(cbuf);
422     g_string_free(gstrEPow, TRUE);
423     TRACEIT(10,"BytesToHumanityString		end");
424 }
425 
426 
checkStringIsPath(char * cpPath)427 gboolean checkStringIsPath(char *cpPath){
428     TRACEIT(10,"checkStringIsPath		start");
429     if (cpPath == NULL){
430         TRACEIT(2,"checkStringIsPath		end, Parameter NULL");
431         return FALSE;
432     }
433     gboolean bRet=FALSE;
434 
435     bRet = g_file_test( cpPath, G_FILE_TEST_IS_DIR);
436 
437     TRACEIT(10,"checkStringIsPath		end");
438     return bRet;
439 }
440 
441 
completionReturnFunction(GString * gstrString)442 char *completionReturnFunction(GString *gstrString){
443     return gstrString->str;
444 }
445 
446 
GListFreeGStrings(gpointer data,gpointer user_data)447 void *GListFreeGStrings (gpointer data, gpointer user_data){
448     TRACEIT(10,"GListFreeGStrings		start");
449     if (data == NULL){
450         TRACEIT(2,"GListFreeGStrings		end, Parameter NULL");
451         return NULL;
452     }
453     GString *gstrBuf = (GString *)data;
454     g_string_free(gstrBuf, TRUE);
455     TRACEIT(10,"GListFreeGStrings		end");
456     return NULL;
457 }
458 
459 
completePath(char * cpText,char ** ToComplete)460 int completePath(char *cpText, char **ToComplete){
461     //ToComplete has to be freed !!!
462     //return: -1:error, 0=all ready path, 1=completed but no directory, 2=completed valid directory
463     TRACEIT(10,"completePath		start");
464 
465     if (cpText == NULL || ToComplete == NULL){
466         TRACEIT(2,"completePath		end, parameter null");
467         return -1;
468     }
469 
470 	struct myLocationStruct locationStru;
471 	GString *gstrNames=g_string_sized_new(10);
472 	GString *gstrPath=g_string_sized_new(10);
473     struct FileAttributs attribs;
474     GList *listDirs=NULL;
475     GCompletion *completion=NULL;
476 
477     g_string_assign(gstrPath, cpText);
478     if (0 == GetPathFromPathWithName (gstrPath, 0)){ //0=Already Path
479         g_string_free(gstrNames,TRUE);
480         g_string_free(gstrPath,TRUE);
481         TRACEIT(10,"completePath		end");
482         return 0;
483     }
484 
485     openLocation(gstrPath->str, &locationStru, FALSE, FALSE);
486 
487     checkPathForLastChar(gstrPath);
488 
489     while ( 1==getNextItemInLocation(&locationStru, gstrNames, FALSE, FALSE) ){
490         fileAttributesGet(&attribs, gstrPath->str, gstrNames->str);
491         if (attribs.type_d==4 && strcmp(gstrNames->str, ".")!=0 && strcmp(gstrNames->str, "..")!=0){
492             g_string_prepend(gstrNames, gstrPath->str);
493             listDirs = g_list_append(listDirs, g_string_new(gstrNames->str));
494         }
495         if (attribs.gstrLinkTarget){
496             g_string_free(attribs.gstrLinkTarget, TRUE);
497         }
498     }
499     closeLocation(&locationStru);
500 
501     if (listDirs){
502         completion = g_completion_new ( (GCompletionFunc) completionReturnFunction);
503         g_completion_add_items (completion, listDirs);
504         g_completion_complete (completion, cpText, ToComplete);
505         g_completion_clear_items (completion);
506         g_completion_free(completion);
507 
508         g_list_foreach(listDirs, (GFunc) GListFreeGStrings, NULL);
509         g_list_free(listDirs);
510     }
511     g_string_free(gstrNames,TRUE);
512 
513     if (*ToComplete){
514         g_string_assign(gstrPath, *ToComplete);
515         if (TRUE == checkStringIsPath(gstrPath->str)){
516             g_string_free(gstrPath,TRUE);
517             TRACEIT(10,"completePath		end");
518             return 2; //valid directory
519         }
520     }
521     g_string_free(gstrPath,TRUE);
522     TRACEIT(10,"completePath		end");
523     return 1; //no directory
524 
525 
526      //I thought I could use g_filename_completer_get_completion_suffix to complete the string in the Pathentrybox
527     //but it did not work (I dont know why, did I something wrong in the code below?)
528 /*    if ( (event->keyval == GDK_Tab)){
529 
530         GFilenameCompleter *completer = g_filename_completer_new ();
531         char *cText=NULL;
532         g_object_get(PathEntryBox[leftORright],"text",&cText,NULL);
533         if (completer == NULL){
534 
535         }
536         g_filename_completer_set_dirs_only(completer, TRUE);
537         gchar *cCompleted = g_filename_completer_get_completion_suffix (completer, cText);
538         if (cText){
539 
540 
541             g_free(cText);
542         }
543         if (cCompleted){
544 
545 
546             g_free(cCompleted);
547         }
548         else{
549 
550         }
551         g_object_unref (completer);
552         return TRUE;
553     }*/
554 }
555 
556 
scrollListStoreToName_Helper(GtkTreeModel * model,GtkTreePath * path,GtkTreeIter * iter,struct AllInformationAllUseStruct * stru)557 gboolean scrollListStoreToName_Helper (GtkTreeModel *model, GtkTreePath *path, GtkTreeIter *iter, struct AllInformationAllUseStruct *stru){
558     TRACEIT(10,"scrollListStoreToName_Helper		start");
559     int *Column = (int *)stru->Info_2;
560     char *NameToFind = (char *)stru->Info_3;
561     gchar *Name=NULL;
562     GtkTreeSelection	*selection=NULL;
563 
564     gtk_tree_model_get(model, iter, *Column, &Name, -1);
565     if (Name){
566         if (strcmp(Name,NameToFind)==0){
567             gtk_tree_view_scroll_to_cell ((GtkTreeView *)stru->Info_4,path,NULL,TRUE,0.0,0.0);
568             selection  = gtk_tree_view_get_selection(GTK_TREE_VIEW((GtkTreeView *)stru->Info_4));
569             gtk_tree_selection_select_iter(selection, iter);
570             g_free(Name);
571             TRACEIT(10,"scrollListStoreToName_Helper		ends");
572             return TRUE;
573         }
574     }
575     if (Name)
576         g_free(Name);
577     TRACEIT(10,"scrollListStoreToName_Helper		ends");
578     return FALSE;
579 }
580 
581 
scrollListStoreToName(GtkTreeView * listView,GtkListStore * listStore,GString * NameToScroll,int ColumnToSearch)582 int scrollListStoreToName(GtkTreeView *listView, GtkListStore *listStore, GString *NameToScroll, int ColumnToSearch){
583     TRACEIT(10,"scrollListStoreToName		start");
584     struct AllInformationAllUseStruct stru;
585     int Column = ColumnToSearch;
586     stru.Info_2=(void *)&Column;
587     stru.Info_3=(void *)NameToScroll->str;
588     stru.Info_4=(void *)listView;
589 
590     gtk_tree_model_foreach(GTK_TREE_MODEL(listStore),(GtkTreeModelForeachFunc) scrollListStoreToName_Helper,&stru);
591     TRACEIT(10,"scrollListStoreToName		end");
592     return 1;
593 }
594 
595 
getHomeDirectory(GString * HomeDir)596 int getHomeDirectory(GString *HomeDir){
597 	TRACEIT(10,"getHomeDirectory		start");
598 
599 	char *buf=NULL;
600 	if (HomeDir == NULL){
601         TRACEIT(2,"getHomeDirectory		Parameter NULL");
602 		return -1;
603 	}
604 	buf = getenv("HOME");
605 	g_string_assign(HomeDir, buf);
606 	TRACEIT(10,"getHomeDirectory		end");
607 	return 0;
608 }
609 
610 
checkPathForLastChar(GString * Path)611 void checkPathForLastChar(GString *Path){
612 	TRACEIT(11,"checkPathForLastChar		start");
613 	if (Path->str[strlen(Path->str)-1] != '/'){
614 		g_string_append_c(Path,'/');
615 	}
616 	TRACEIT(11,"checkPathForLastChar		ends");
617 }
618 
619 
checkPathForLastCharOtherwise(GString * Path)620 void checkPathForLastCharOtherwise(GString *Path){
621 	TRACEIT(11,"checkPathForLastCharOtherwise		start");
622 	if (strlen(Path->str) >1){
623 		if (Path->str[strlen(Path->str)-1] == '/'){
624 			g_string_set_size(Path,strlen(Path->str)-1);
625 		}
626 	}
627 	TRACEIT(11,"checkPathForLastCharOtherwise		ends");
628 }
629 
630 
cutSpacesFromGString(GString * ToCut)631 void cutSpacesFromGString(GString *ToCut){
632 	TRACEIT(10,"cutSpacesFromGString		start");
633 	cutCharFromGString(ToCut, ' ',0);
634 	TRACEIT(10,"cutSpacesFromGString		ends");
635 }
636 
cutLeadingSpacesFromGString(GString * ToCut)637 void cutLeadingSpacesFromGString(GString *ToCut){
638     TRACEIT(10,"cutLeadingSpacesFromGString		start");
639 	cutCharFromGString(ToCut, ' ',1);
640 	TRACEIT(10,"cutLeadingSpacesFromGString		ends");
641 }
642 
cutTapsFromGString(GString * ToCut)643 void cutTapsFromGString(GString *ToCut){
644 	TRACEIT(10,"cutTapsFromGString		start");
645 	cutCharFromGString(ToCut, '\t',0);
646 	TRACEIT(10,"cutTapsFromGString		ends");
647 }
648 
649 
cutCharFromGString(GString * ToCut,char thechar,int OnlyLeadingOnes)650 void cutCharFromGString(GString *ToCut, char thechar, int OnlyLeadingOnes){
651 	TRACEIT(10,"cutCharFromGString		start");
652 	int i;
653 	for (i=0; i<strlen(ToCut->str); i++){
654 		if (ToCut->str[i] == thechar){
655 			g_string_erase(ToCut,i,1);
656 			i--;
657 		}
658 		else if (OnlyLeadingOnes == 1){
659             return;
660 		}
661 	}
662 	TRACEIT(10,"cutCharFromGString		end");
663 }
664 
665 
getLastStringInPath(GString * Path)666 void getLastStringInPath(GString *Path){ //removes the path and leaves the single name
667 	TRACEIT(10,"getLastStringInPath		start");
668 
669 	GString *gstrbuf = g_string_new(Path->str);
670 
671 	if (strcmp(Path->str,"/")==0){
672 		g_string_free(gstrbuf,TRUE);
673 		return;
674 	}
675 	checkPathForLastCharOtherwise(gstrbuf);
676 	char *buf = strrchr(Path->str,'/');
677 
678 	if (buf == NULL){
679 		TRACEIT(9,"getLastStringInPath		ends NO");
680 		g_string_free(gstrbuf,TRUE);
681 		return;
682 	}
683 
684 	buf++;
685 	if (buf == '\0'){
686 		TRACEIT(3,"getLastStringInPath		ends SHOULD NEVER BE");
687 		g_string_free(gstrbuf,TRUE);
688 		return;
689 	}
690 
691 	g_string_assign(Path,buf);
692 	g_string_free(gstrbuf,TRUE);
693 	TRACEIT(10,"getLastStringInPath		ends");
694 }
695 
696 
GetPathFromPathWithName(GString * PathAndName,int iDoIt)697 int GetPathFromPathWithName(GString *PathAndName, int iDoIt){
698     //if iDoIt == 1, then the last string will be cut off wether or not the string in PathAndName is allready a path (ending /)
699     //it iDoIt == 0, then this fuctions does nothing when the last char in PathAndName is a /
700 
701     //return: -1: Error, 0: already path, 1: Path written to PathAndName
702     //
703 	TRACEIT(10,"GetPathFromPathWithName		start");
704 	if (PathAndName == NULL){
705 		TRACEIT(10,"GetPathFromPathWithName		end 1");
706 		return -1;
707 	}
708 	GString *gstrBuf = g_string_new(PathAndName->str);
709 
710 	if ( iDoIt == 0 && (gstrBuf->str)[strlen(gstrBuf->str)-1]=='/'){ //last char in PathAndName is a /, so it is all ready a path.
711         g_string_free(gstrBuf, TRUE);
712         return 0;
713 	}
714 
715 	getLastStringInPath(gstrBuf);
716 
717 	g_string_truncate(PathAndName, strlen(PathAndName->str)-strlen(gstrBuf->str)-1);
718 
719     if (strlen(PathAndName->str) == 0){ //we are in the root directory
720         g_string_assign(PathAndName, "/");
721     }
722 	g_string_free(gstrBuf, TRUE);
723 	TRACEIT(10,"GetPathFromPathWithName		end");
724 	return 1;
725 }
726 
727 
cutStringToNChars(GString * Text,int Size)728 void cutStringToNChars(GString *Text,int Size){
729 	TRACEIT(10,"cutStringToNChars		start");
730 	if (strlen(Text->str)<=Size){
731 		TRACEIT(10,"cutStringToNChars		ends");
732 		return;
733 	}
734 	g_string_truncate(Text,Size);
735 	g_string_append(Text, "...");
736 
737 	TRACEIT(10,"cutStringToNChars		ends");
738 }
739 
740 
cleverCutStringToNChars(GString * ToCut,char * textToAddBetween,int Size)741 void cleverCutStringToNChars(GString *ToCut, char *textToAddBetween, int Size){
742     //textToAddBetween can be NULL
743     //devides ToCut into three sections. section 1 and 3 are half the size of Size and are put together to the new string,
744     //which ends up not larger than Size in the case textToAddBetween is empty or NULL.
745     //Section 1 comes from the beginning of ToCut while section 3 comes from the end.
746     //Section 2 are the chars between section 1 and 3, and are deleted or exchanged by textToAddBetween.
747 
748 	TRACEIT(10,"cleverCutStringToNChars		start");
749 	if (ToCut==NULL){
750 	    TRACEIT(10,"cleverCutStringToNChars		ends, parameter NULL");
751 	    return;
752 	}
753 	if (strlen(ToCut->str)<=Size){
754 		TRACEIT(10,"cleverCutStringToNChars		ends, nothing to do");
755 		return;
756 	}
757     int wholeSize = strlen(ToCut->str);
758     int pos = Size/2;
759     int lenToCut = wholeSize-Size;
760 
761     g_string_erase (ToCut, pos, lenToCut);
762     if (textToAddBetween != NULL){
763         g_string_insert (ToCut, pos+1, textToAddBetween);   //not sure about the +1 here.
764     }
765 
766     TRACEIT(10,"cleverCutStringToNChars		end");
767 }
768 
769 
myTokenFunction(GString * gstrCompleteString,int TokenNumber,char * divider,GString * gstrToken)770 int myTokenFunction (GString *gstrCompleteString, int TokenNumber, char *divider, GString *gstrToken){
771     //gstrToken must be a valid GString, we only assign a new string to it
772     TRACEIT(10,"myTokenFunction		start");
773     if (gstrCompleteString == 0 || divider==NULL || gstrToken == NULL){
774         TRACEIT(2,"myTokenFunction      Parameter NULL");
775         return -1;
776     }
777 
778     if (strstr(gstrCompleteString->str, divider) == 0){
779         g_string_assign(gstrToken, gstrCompleteString->str);
780         TRACEIT(10,"myTokenFunction		ends, no divider in string");
781         if (TokenNumber == 0){
782             return 1;
783         }
784         else{
785             return -1;
786         }
787     }
788     int i;
789     char *buf = malloc( sizeof(char *) * (strlen(gstrCompleteString->str)+2) );
790     char *start=NULL;
791     char *end=NULL;
792 
793     strcpy(buf,gstrCompleteString->str);
794 
795     start = buf;
796     for( i=0; i<TokenNumber; i++){
797         start = strstr(start, divider);
798         if (start == NULL){
799             free(buf);
800             TRACEIT(10,"myTokenFunction		ends, no more tokens");
801             return -1;
802         }
803         start += strlen(divider);
804     }
805     end = strstr(start,divider);
806     if (end == NULL){
807         end = &(buf[strlen(buf)]);
808     }
809     *end = '\0';
810     g_string_assign(gstrToken, start);
811 
812     free(buf);
813     TRACEIT(10,"myTokenFunction		ends");
814     return 1;
815 }
816 
817 
AddNewGStringToDPointer(GString *** gstrTPString,int OldCount,GString * NewOne)818 int AddNewGStringToDPointer(GString ***gstrTPString,int OldCount, GString *NewOne){
819 	TRACEIT(10,"AddNewGStringToDPointer		start");
820 	//allocates new memory and copies all entries from gstrTPString and the NewOne to the new memory
821 	//and frees the old, THE GSTRING IN NEWONE IS COPIED !!!!!!!! THIS MEANS A NEW GSTRING IS CREATED!!
822 	int i;
823 	GString *NewString;
824 	GString **NewDPointer=NULL;
825 	NewDPointer	= malloc(sizeof(GString *) * (OldCount +1));
826 	if (NewDPointer == NULL){
827 		TRACEIT(1,"AddNewGStringToDPointer		end, NewPointer = NULL, ERROR allocting memory");
828 		return -1;
829 	}
830 	for (i=0;i<OldCount;i++){
831 		NewDPointer[i] = (*gstrTPString)[i];
832 	}
833 	if (NewOne == NULL){
834 		NewDPointer[OldCount] = NULL;
835 	}
836 	else{
837 		NewString = g_string_new(NewOne->str);
838 		NewDPointer[OldCount]=NewString;
839 	}
840 	if (*gstrTPString != NULL)
841 		free(*gstrTPString);
842 
843 	*gstrTPString = NewDPointer;
844 	TRACEIT(10,"AddNewGStringToDPointer		end");
845 	return 0;
846 }
847 
848 
AddNewGUInt64ToPointerArray(guint64 ** intArray,int OldCount,guint64 NewOne)849 int AddNewGUInt64ToPointerArray(guint64 **intArray, int OldCount, guint64 NewOne){
850 	TRACEIT(10,"ToPointerArray		start");
851 	//allocates new memory and copies all entries from intArray and the NewOne to the new memory
852 	//and frees the old.
853 	int i;
854 	guint64 *NewPointer=NULL;
855 	NewPointer	= malloc(sizeof(guint64) * (OldCount +1));
856 	if (NewPointer == NULL){
857 		TRACEIT(1,"ToPointerArray		end, NewPointer, ERROR allocating memory");
858 		return -1;
859 	}
860 	for (i=0;i<OldCount;i++){
861 		NewPointer[i] = (*intArray)[i];
862 	}
863 
864 	NewPointer[OldCount]=NewOne;
865 
866 	if (*intArray != NULL)
867 		free(*intArray);
868 
869 	*intArray = NewPointer;
870 	TRACEIT(10,"ToPointerArray		end");
871 	return 0;
872 }
873 
874 
KeepWindowOnTop(GtkWidget * widget,GdkEvent * event,gpointer user_data)875 gboolean KeepWindowOnTop (GtkWidget *widget, GdkEvent  *event, gpointer   user_data){
876 	TRACEIT(10,"KeepWindowOnTop		start");
877 	if (event->window_state.type == 32){		//GDK_WINDOW_STATE      = 32
878 		gtk_window_deiconify ((GtkWindow *)user_data);
879 	}
880 	TRACEIT(10,"KeepWindowOnTop		end");
881 	return TRUE;
882 }
883 
884 
DeleteNotification(GtkWidget * widget,GdkEvent * event,gpointer user_data)885 gboolean DeleteNotification (GtkWidget *widget, GdkEvent  *event, gpointer   user_data){
886     int *notification = (int *)user_data;
887     *notification = 0;  //dialog destroyed, this, however has nothing to do whith mainloop, but just destroys the widget
888     return FALSE;   //when this is TRUE, the widget will stay even when the user tries to close it. And because of our notification we d not close it either
889 }
890 
891 
CreateMyWindow(char * title,GtkWindow * parent,int x,int y,gboolean bResizeable,gboolean bDestroyWithParent,gboolean bKeepOnTop,int * iDeleteNotification,gboolean bParentInteraction,gboolean bDeletable)892 GtkWidget *CreateMyWindow(char *title, GtkWindow *parent, int x, int y, gboolean bResizeable, gboolean bDestroyWithParent, gboolean bKeepOnTop, int *iDeleteNotification, gboolean bParentInteraction, gboolean bDeletable ){
893     //iDeleteNotification becomes 1 when window is running and 0 when it receives the destroy-signal
894 	TRACEIT(10,"CreateMyWindow		start");
895 	GtkWidget *dialog;
896 	dialog = gtk_window_new(GTK_WINDOW_TOPLEVEL);
897 	//gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog),TRUE);
898 	gtk_window_set_deletable((GtkWindow *)dialog,bDeletable);
899 	gtk_window_set_title((GtkWindow *)dialog,title);
900 	gtk_window_set_default_size((GtkWindow *)dialog,x,y);
901 	gtk_window_set_resizable((GtkWindow *)dialog,bResizeable);
902 
903 	if (parent != NULL){
904 	    if (bParentInteraction){
905             gtk_window_set_modal((GtkWindow *)dialog,TRUE); //modal dialogs can be connected to other widgets with ..set_transient_for
906 	    }                                                      //Modal windows prevent interaction with other windows in the same application
907 		gtk_window_set_transient_for((GtkWindow *)dialog,parent);   //connects, in a way, the window with a parent window
908 
909 		if (bDestroyWithParent){
910             gtk_window_set_destroy_with_parent((GtkWindow *)dialog, TRUE);
911 		}
912 	}
913 
914     if (bKeepOnTop)
915         g_signal_connect (dialog,"window-state-event",G_CALLBACK(KeepWindowOnTop),(void *)dialog);
916     if (iDeleteNotification){
917         //printf("CreateMyWindow      Deletenotification enabled\n");
918         *iDeleteNotification = 1;  //dialog valid
919         //g_signal_connect (dialog,"destroy-event",G_CALLBACK(DeleteNotification),(void *)iDeleteNotification); //destroy-eventg hooks to the gdkwindow, but this may be destroyed from the widget automaticaly and so, no windget owns this window. That means that we do not receive this signal
920         g_signal_connect (dialog,"delete-event",G_CALLBACK(DeleteNotification),(void *)iDeleteNotification);    //his event hooks to the Widget
921     }
922 
923 	TRACEIT(10,"CreateMyWindow		end");
924 	return dialog;
925 }
926 
927 
CreateMyDialog(char * title,GtkWindow * parent,int iButtons,char * respText1,int resp1,char * respText2,int resp2,char * respText3,int resp3,gboolean bResizeable)928 GtkWidget *CreateMyDialog(char *title, GtkWindow *parent, int iButtons, char *respText1, int resp1, char *respText2, int resp2, char *respText3, int resp3, gboolean bResizeable){
929 	TRACEIT(10,"CreateMyDialog		start");
930 
931 	GtkWidget *dialog=NULL;
932 	if (iButtons == 1){
933 		dialog = gtk_dialog_new_with_buttons(title,parent,GTK_DIALOG_DESTROY_WITH_PARENT,respText1,resp1,NULL);
934 	}
935 	else if (iButtons == 2){
936 		dialog = gtk_dialog_new_with_buttons(title,parent,GTK_DIALOG_DESTROY_WITH_PARENT,respText1,resp1,respText2,resp2,NULL);
937 	}
938 	else if (iButtons == 3){
939 	    dialog = gtk_dialog_new_with_buttons(title,parent,GTK_DIALOG_DESTROY_WITH_PARENT,respText1,resp1,respText2,resp2,respText3,resp3,NULL);
940 	}
941 
942 	//printf("creating dialog: %p, text1:%s--- text2:%s--- \n",dialog, respText1, respText2 );
943 
944 	gtk_window_set_deletable((GtkWindow *)dialog,FALSE);
945 	gtk_window_set_resizable((GtkWindow *)dialog,bResizeable);
946 
947 	g_signal_connect (dialog,"window-state-event",G_CALLBACK(KeepWindowOnTop),(void *)dialog);
948 	TRACEIT(10,"CreateMyDialog		end");
949 	return dialog;
950 }
951 
952 
MyMessageWidget(GtkWindow * parrent,char * title,char * message)953 void MyMessageWidget(GtkWindow *parrent, char *title, char* message){
954 	TRACEIT(10,"MyMessageWidget		start");
955 	TRACEIT(10,"MyMessageWidget, message:");
956 	TRACEIT(10,message);
957 	GtkWidget *label;
958 	GtkWidget *dialog;
959 	GtkWidget *scrollwidget;
960 
961     //TRACEIT (5, "MyMessageWidget, message:");
962     //TRACEIT (5, message);
963 
964 	dialog = CreateMyDialog(title,parrent,1,"Ok",GTK_RESPONSE_ACCEPT,"Stop execution",GTK_RESPONSE_REJECT,NULL, 0, TRUE);
965     label = gtk_label_new(message);
966 
967     gtk_label_set_selectable ( (GtkLabel *)label, TRUE);
968     gtk_label_set_line_wrap ( (GtkLabel *)label, TRUE);
969 
970     //gtk_window_set_default_size ( (GtkWindow *)dialog, 300, 200);
971     gtk_window_resize ( (GtkWindow *)dialog, 300, 200);
972     //gtk_window_set_resizable((GtkWindow *)dialog,FALSE);
973 
974 
975     if (strlen(message)>100){
976         scrollwidget = gtk_scrolled_window_new(NULL,NULL);
977         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwidget),GTK_POLICY_AUTOMATIC,GTK_POLICY_AUTOMATIC);
978         gtk_scrolled_window_add_with_viewport ( (GtkScrolledWindow *)scrollwidget, label);
979         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),scrollwidget);
980     }
981     else{
982         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),label);
983     }
984 
985 	gtk_widget_show_all(dialog);
986 	gtk_dialog_run(GTK_DIALOG(dialog));
987 	gtk_widget_destroy(dialog);
988 	TRACEIT(10,"MyMessageWidget		end");
989 }
990 
991 
MyEasyInputWidget(GtkWindow * parrent,char * title,char * text,GString * gstrUserText)992 int MyEasyInputWidget (GtkWindow *parrent, char *title, char *text, GString *gstrUserText){
993     // opens a little dialog in which the user can give us a little text, think of the rename - dialog.
994     // wahtever stays in gstrUserText is displayed in the input area as a predefined text.
995     // return values: 0: User OK     1: User Cancel   <0: Error
996     TRACEIT(10,"MyEasyInputWidget		start");
997     if (NULL == title || NULL==text || NULL==gstrUserText){
998             TRACEIT(3,"MyEasyInputWidget		end, one or more parameters NULL");
999             return -1;
1000     }
1001 	GtkWidget *dialog, *label;
1002 	GtkWidget *EntryBox;
1003 	GString *gstrbuf = g_string_sized_new(10);
1004 
1005     dialog = CreateMyDialog(title, parrent, 2, "Ok",GTK_RESPONSE_ACCEPT,"Cancel",GTK_RESPONSE_REJECT, NULL, 0, FALSE);
1006 
1007 	label = gtk_label_new(text);
1008 	gtk_misc_set_alignment ( (GtkMisc *)label, 0.0f, 0.0f);
1009     gtk_misc_set_padding ( (GtkMisc *)label, 4, 0);
1010 	gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),label);
1011 	EntryBox = gtk_entry_new();
1012 	gtk_entry_set_max_length(GTK_ENTRY(EntryBox),MAXENTRYBOX);
1013 	gtk_editable_set_editable(GTK_EDITABLE(EntryBox),TRUE);
1014 	g_signal_connect (EntryBox, "activate",G_CALLBACK(MyEasyInputWidget_EntryBoxActivate),dialog);
1015 	gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),EntryBox);
1016 	g_object_set(EntryBox, "text", gstrUserText->str, NULL);
1017 
1018 	gtk_widget_show_all(dialog);
1019 
1020 	if (gtk_dialog_run(GTK_DIALOG(dialog))== GTK_RESPONSE_ACCEPT){
1021 		g_object_get(EntryBox, "text", gstrbuf, NULL);
1022 		g_string_assign(gstrUserText,gstrbuf->str);
1023 
1024         gtk_widget_destroy(dialog);
1025         TRACEIT(10,"MyEasyInputWidget		end 0");
1026         g_string_free(gstrbuf, TRUE);
1027         return 0;
1028 	}
1029 	gtk_widget_destroy(dialog);
1030 	TRACEIT(10,"MyEasyInputWidget		end 1");
1031 	g_string_free(gstrbuf, TRUE);
1032     return 1;
1033 }
1034 
1035 
MyEasyInputWidget_Two(GtkWindow * parrent,char * title,char * text1,GString * gstrUserText1,char * text2,GString * gstrUserText2,char * button0,char * button1,char * button2,char * button3)1036 int MyEasyInputWidget_Two (GtkWindow *parrent, char *title, char *text1, GString *gstrUserText1, char *text2, GString *gstrUserText2, char *button0, char *button1, char *button2, char *button3 ){
1037     // opens a little dialog in which the user can give us a little text, think of the rename - dialog.
1038 	// but this widget has up to two entry boxes where to enter text.
1039 	// and up to 4 buttons
1040 	// text1 must not be NULL
1041 	// while gstrUserText1, text2 and gstrUserText2 may be NULL, then, there is only one entrybox or none
1042 	// text2 and gstrUserText2 may only be NULL at the same time
1043     // wahtever stays in gstrUserText is displayed in the input area as a predefined text.
1044 
1045     // when the ENTER is hit in one of the entryboxes 0 (means the first button) is returned.
1046 
1047     //The Gstrings are updated only when first button is pressed
1048     // return values: 0: User OK     1: User Cancel   2: User Cancel All 	<0: Error
1049     TRACEIT(10,"MyEasyInputWidget_Two		start");
1050     if (NULL == title || NULL==text1 /*|| NULL==gstrUserText1*/ ){
1051             TRACEIT(3,"MyEasyInputWidget_Two		end, one or more parameters NULL");
1052             return -1;
1053     }
1054     if (NULL == button0 || NULL==button1 /*|| NULL==gstrUserText1*/ ){
1055             TRACEIT(3,"MyEasyInputWidget_Two		end, at lease the first two buttons have to be defined");
1056             return -1;
1057     }
1058 
1059 	GtkWidget *dialog, *label1, *label2;
1060 
1061 	GtkWidget *EntryBox1=NULL, *EntryBox2=NULL;
1062 	GString *gstrbuf1 = g_string_sized_new(10);
1063 	GString *gstrbuf2 = g_string_sized_new(10);
1064 	int iText2Valid=0;
1065 	int iText1Valid=0;
1066 
1067     //dialog = CreateMyDialog(title, parrent, 2, "ok",GTK_RESPONSE_ACCEPT,"cancel",GTK_RESPONSE_REJECT, FALSE);
1068     if (button0 && button1 && button2 && button3){
1069         dialog = gtk_dialog_new_with_buttons(title,parrent,GTK_DIALOG_DESTROY_WITH_PARENT, button0, 0, button1, 1, button2, 2, button3, 3, NULL);
1070     }
1071     else if (button0 && button1 && button2 && button3==NULL){
1072         dialog = gtk_dialog_new_with_buttons(title,parrent,GTK_DIALOG_DESTROY_WITH_PARENT, button0, 0, button1, 1, button2, 2, NULL);
1073     }
1074     else if(button0 && button1 && button2==NULL && button3==NULL){
1075         dialog = gtk_dialog_new_with_buttons(title,parrent,GTK_DIALOG_DESTROY_WITH_PARENT, button0, 0, button1, 1, NULL);
1076     }
1077     else{
1078         dialog = gtk_dialog_new_with_buttons(title,parrent,GTK_DIALOG_DESTROY_WITH_PARENT,"Ok",GTK_RESPONSE_ACCEPT,"Cancel all",2,"Cancel",1,NULL);
1079     }
1080 	gtk_window_set_deletable((GtkWindow *)dialog,FALSE);
1081 	gtk_window_set_resizable((GtkWindow *)dialog,TRUE);
1082 	g_signal_connect (dialog,"window-state-event",G_CALLBACK(KeepWindowOnTop),(void *)dialog);
1083 
1084 
1085 	label1 = gtk_label_new(text1);
1086 	gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),label1);
1087 	gtk_misc_set_alignment ( (GtkMisc *)label1, 0.0f, 0.0f);
1088     gtk_misc_set_padding ( (GtkMisc *)label1, 4, 0);
1089 	if (gstrUserText1!=NULL){
1090         EntryBox1 = gtk_entry_new();
1091         gtk_entry_set_max_length(GTK_ENTRY(EntryBox1),MAXENTRYBOX);
1092         gtk_editable_set_editable(GTK_EDITABLE(EntryBox1),TRUE);
1093         g_signal_connect (EntryBox1, "activate",G_CALLBACK(MyEasyInputWidget_EntryBoxActivate),dialog);
1094         gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),EntryBox1);
1095         g_object_set(EntryBox1, "text", gstrUserText1->str, NULL);
1096         iText1Valid=1;
1097 	}
1098 
1099 	if (text2 != NULL && gstrUserText2 != NULL){
1100 		label2 = gtk_label_new(text2);
1101 		gtk_misc_set_alignment ( (GtkMisc *)label2, 0.0f, 0.0f);
1102         gtk_misc_set_padding ( (GtkMisc *)label2, 4, 0);
1103 		gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),label2);
1104 		EntryBox2 = gtk_entry_new();
1105 		gtk_entry_set_max_length(GTK_ENTRY(EntryBox2),MAXENTRYBOX);
1106 		gtk_editable_set_editable(GTK_EDITABLE(EntryBox2),TRUE);
1107 		g_signal_connect (EntryBox2, "activate",G_CALLBACK(MyEasyInputWidget_EntryBoxActivate),dialog);
1108 		gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox),EntryBox2);
1109 		g_object_set(EntryBox2, "text", gstrUserText2->str, NULL);
1110 		iText2Valid=1;
1111 	}
1112 
1113 	gtk_widget_show_all(dialog);
1114     int ret=gtk_dialog_run(GTK_DIALOG(dialog));
1115 	if (ret == 0 || ret == GTK_RESPONSE_ACCEPT){    //hitting enter in one of the entryboxes returns GTK_RESPONSE_ACCEPT
1116 	    if (iText1Valid==1){
1117             g_object_get(EntryBox1, "text", gstrbuf1, NULL);
1118             g_string_assign(gstrUserText1,gstrbuf1->str);
1119 	    }
1120 		if (iText2Valid==1){
1121 		    g_object_get(EntryBox2, "text", gstrbuf2, NULL);
1122 			g_string_assign(gstrUserText2,gstrbuf2->str);
1123 		}
1124 
1125         gtk_widget_destroy(dialog);
1126         TRACEIT(10,"MyEasyInputWidget_Two		end 0");
1127         g_string_free(gstrbuf2, TRUE);
1128         g_string_free(gstrbuf1, TRUE);
1129         return 0;   //Do not return GTK_RESPONSE_ACCEPT, but 0, this means the first button
1130 	}/*
1131 	else if(ret==2){
1132         g_string_free(gstrbuf2, TRUE);
1133         g_string_free(gstrbuf1, TRUE);
1134         gtk_widget_destroy(dialog);
1135         return ret;
1136 	}*/
1137 
1138 	gtk_widget_destroy(dialog);
1139 	TRACEIT(10,"MyEasyInputWidget_Two		end 1");
1140 	g_string_free(gstrbuf2, TRUE);
1141     g_string_free(gstrbuf1, TRUE);
1142     return ret;
1143 }
1144 
1145 
AddMyIconsToWindow(GtkWindow * Window)1146 void AddMyIconsToWindow(GtkWindow *Window){
1147 	TRACEIT(10,"AddMyIconsToWindow		start");
1148     GString *gstrbuf = g_string_sized_new(20);
1149     GString *gstrbuf2 = g_string_sized_new(20);
1150 	char *cppSizes[] = {"16x16","48x48","64x64"}; //,"128x128","160x160"};	//Do not forgett to de- or increment for-loop when changing Items in this pointerarray
1151 	int i=0;								//On IceWM: when we add more than 64x64 (or 3 Items) there was no Icon at all
1152 
1153     getHomeDirectory(gstrbuf);
1154     checkPathForLastChar(gstrbuf);
1155     g_string_append(gstrbuf,ICONNAMEWITHOUTSIZE);
1156 
1157 	GError *err=NULL;
1158 	GList *pMyIconList=NULL;
1159 	GdkPixbuf *pixMainWindowIcon=NULL;
1160 
1161 	for (i=0; i<3; i++){
1162 	    g_string_assign(gstrbuf2, gstrbuf->str);
1163 		g_string_append(gstrbuf2, cppSizes[i]);
1164 		g_string_append(gstrbuf2, ".gif");
1165 
1166 		pixMainWindowIcon = gdk_pixbuf_new_from_file (gstrbuf2->str, &err);
1167 
1168 		if (err){
1169 			TRACEIT(6,"AddMyIconsToWindow		error Loading Icon from file:");
1170 			TRACEIT(6,gstrbuf2->str);
1171 			g_error_free(err);
1172 			err=NULL;
1173 		}
1174 		if (pixMainWindowIcon != NULL){
1175 			pMyIconList = g_list_append(pMyIconList, (void *)pixMainWindowIcon);
1176 		}
1177 	}
1178 	gtk_window_set_icon_list (Window, pMyIconList);
1179 	g_list_free(pMyIconList);
1180 	g_string_free(gstrbuf,TRUE);
1181 	g_string_free(gstrbuf2,TRUE);
1182 
1183 	TRACEIT(10,"AddMyIconsToWindow		end");
1184 }
1185 
1186 
MyEasyInputWidget_EntryBoxActivate(GtkEntry * entry,gpointer data)1187 void MyEasyInputWidget_EntryBoxActivate(GtkEntry *entry, gpointer data){
1188 	TRACEIT(10,"MyEasyInputWidget_EntryBoxActivate		start");
1189 
1190 	gtk_dialog_response (GTK_DIALOG(data), GTK_RESPONSE_ACCEPT);
1191 
1192 	//This is just when the user hits the enterbutton, then the OKButton is activated
1193 	//!!!!!!!!  Be carefull, this function is called from many functions not only the NewDirectory-thing   !!!!!
1194 	TRACEIT(10,"MyEasyInputWidget_EntryBoxActivate		ends");
1195 }
1196 
1197 /*
1198 gboolean AddESCButtonToAMainWidget_SignalHandler(GtkWidget *widget, GdkEventKey *event, GtkWidget *TheWidget){
1199 	TRACEIT(10,"AddESCButtonToAMainWidget_SignalHandler		start");
1200 	guint modifiers;
1201 
1202 	modifiers = gtk_accelerator_get_default_mod_mask ();
1203 
1204     if ( (event->keyval == GDK_Escape)){
1205 	    gtk_widget_destroy (TheWidget);
1206 
1207 		TRACEIT(10,"createUserCommand_oneKeyPressedInView		ends");
1208 		return TRUE;
1209 	}
1210 
1211 	TRACEIT(10,"AddESCButtonToAMainWidget_SignalHandler		end");
1212 	return FALSE;
1213 }
1214 */
1215 
1216 /*
1217 int AddESCButtonToAMainWidget (GtkWidget *TheWidget){
1218     //in the end, this will call gtk_widget_destroy () to TheWidget !
1219     //this means that TheWidget most propably has to be a toplevel widget
1220     TRACEIT(10,"AddESCButtonToAMainWidget		start");
1221     if (TheWidget == NULL){
1222         TRACEIT(10,"AddESCButtonToAMainWidget		parameter NULL");
1223         return -1;
1224     }
1225     g_signal_connect (TheWidget,"key-press-event",G_CALLBACK(AddESCButtonToAMainWidget_SignalHandler),TheWidget);
1226 
1227 
1228     TRACEIT(10,"AddESCButtonToAMainWidget		end");
1229     return 0;
1230 }
1231 */
1232 
1233 
myWrapOf__g_spawn_command_line_sync(const gchar * command_line,gchar ** standard_output,gchar ** standard_error,gint * exit_status,GError ** error,gboolean ThreadSave)1234 gboolean myWrapOf__g_spawn_command_line_sync (const gchar *command_line, gchar **standard_output, gchar **standard_error, gint *exit_status, GError **error, gboolean ThreadSave){
1235     //when ThreadSave is TRUE, all gtk-functions is gdk_threads_enter/leave surrounded.
1236     //if it is FALSE, no gdk_threads_enter/leave functions are called in here
1237 
1238     //if this function is called from within a signal, ThreadSave should be FALSE, as signals are allready threadsafe
1239     //A call to the mentioned functions would cause a deadlock.
1240 
1241     TRACEIT(10,"myWrapOf__g_spawn_command_line_sync		start");
1242     gboolean ret;
1243     time_t currentTime=0;
1244     GString *gstrBuf=g_string_sized_new(30);
1245     unsigned int ID;
1246 
1247     ID = time(&currentTime);
1248 
1249     g_string_printf(gstrBuf, "%d: %s: %s",ID, ctime(&currentTime), command_line);
1250     cutCharFromGString(gstrBuf, '\n',0);
1251     //g_string_append(gstrBuf, "\n\n");
1252     if (ThreadSave){
1253         myWrap_gdk_threads_enter("myWrapOf__g_spawn_command_line_sync 1");
1254     }
1255     textbuffer_commandHistory_addText(gstrBuf->str, NULL);
1256     if (ThreadSave){
1257        myWrap_gdk_threads_leave("myWrapOf__g_spawn_command_line_sync 1");
1258     }
1259 
1260     //char cbuf[100];
1261     //getcwd(cbuf,99);
1262     //printf("current path: %s, command: %s\n",cbuf, command_line);
1263     ret = g_spawn_command_line_sync(command_line,standard_output,standard_error,exit_status,error);
1264 
1265     if (standard_error != NULL && *standard_error != NULL && strlen(*standard_error) > 1 && (( exit_status != NULL && *exit_status != 0) || exit_status==NULL) ){
1266         g_string_printf(gstrBuf, "%d: %s: ERROR: %s",ID, ctime(&currentTime), *standard_error);
1267         cutCharFromGString(gstrBuf, '\n',0);
1268         //g_string_append(gstrBuf, "\n\n\n");
1269         if (ThreadSave){
1270 			//printf("entering gdk_threads\n");
1271             myWrap_gdk_threads_enter("myWrapOf__g_spawn_command_line_sync 2");
1272 			//printf("gdk_threads_entered\n");
1273         }
1274         textbuffer_commandHistory_addText(gstrBuf->str, "error");
1275         if (ThreadSave){
1276 			//printf("going to leave gdk_threas\n");
1277             myWrap_gdk_threads_leave("myWrapOf__g_spawn_command_line_sync 2");
1278 			//printf("myWrap_gdk_threads_leaved\n");
1279         }
1280     }
1281     g_string_free(gstrBuf, TRUE);
1282     TRACEIT(10,"myWrapOf__g_spawn_command_line_sync		end");
1283     return ret;
1284 }
1285 
1286 
textbuffer_commandHistory_addText(char * text,char * tag)1287 void textbuffer_commandHistory_addText (char *text, char *tag){
1288     TRACEIT(10,"textbuffer_commandHistory_addText		start");
1289     //adds text to the end of our g_TextBuffer_CommandHistory,
1290     //tag may be NULL, otherwise the text is added to the buffer with the tag-properties (the tag must be a valid name of an allready connected tag)
1291     //a valid tag e.g. is "error"
1292 
1293     //when no tag is defined \n\n will be appended to text
1294     //wehn tag is defined \n\n\n will be appended to text
1295     if (text == NULL){
1296         TRACEIT(2,"textbuffer_commandHistory_addText		end, Parameter NULL");
1297         return;
1298     }
1299     if (g_TextBuffer_CommandHistory == NULL){
1300         TRACEIT(2,"textbuffer_commandHistory_addText		end, buffer null");
1301         return;
1302     }
1303     GtkTextIter iter;
1304     GString *gstrText = g_string_new(text);
1305     gtk_text_buffer_get_end_iter (g_TextBuffer_CommandHistory, &iter);
1306 
1307     if (tag == NULL){
1308         g_string_append(gstrText, "\n\n");
1309         gtk_text_buffer_insert (g_TextBuffer_CommandHistory, &iter, gstrText->str, -1);
1310     }
1311     else{
1312         g_string_append(gstrText, "\n\n\n");
1313         //the error text will displayed in red, as defined within the tag (here refered by "error" (tagname)), defined in defineGlobals - function
1314         gtk_text_buffer_insert_with_tags_by_name (g_TextBuffer_CommandHistory, &iter, gstrText->str, -1, tag, NULL);
1315     }
1316     g_string_free(gstrText, TRUE);
1317     //gtk_text_iter_free (&iter);   //do not free it, iter is on the stack! (its here just for info)
1318     TRACEIT(10,"textbuffer_commandHistory_addText		end");
1319 }
1320 
1321 
CancelButtonFunctionForIBreak(GtkButton * button,gpointer data)1322 void CancelButtonFunctionForIBreak(GtkButton *button, gpointer data){
1323     //used or buttons which only have to change the ibreak variable (used in search for..., and SizeDialog)
1324 	TRACEIT(10,"CancelButtonFunctionForIBreak		start");
1325 
1326 	int *ibreak = (int *)data;
1327 	*ibreak = 1;
1328 
1329 	TRACEIT(10,"CancelButtonFunctionForIBreak		ends");
1330 }
1331 
1332 
CheckForEndingInEndingList(const char * ccpEndings,const char * ccpSingleEnding)1333 int CheckForEndingInEndingList (const char *ccpEndings, const char *ccpSingleEnding){
1334     //ccpEndings is a string of the form: 'jpg png mp3' as used for listing our endings
1335     //ccpSingleEnding is the ending we want to check ccpEndings for.
1336     //the endings in ccpEndings can be divided by space
1337 
1338     //return:   0 = not found
1339     //          1 = found
1340     //          -1= error
1341     TRACEIT(10,"CheckForEndingInEndingList		start");
1342     char *cpBuf = NULL;
1343     char *cpBuf2 = NULL;
1344     int iRet = 0;
1345     char *cpBufSingleEnding = NULL;
1346 
1347 
1348     cpBufSingleEnding = g_ascii_strdown (ccpSingleEnding, -1);
1349     if (cpBufSingleEnding == NULL){
1350         TRACEIT (2, "CheckForEndingInEndingList Error converting SingleEnding to lower");
1351         return -1;
1352     }
1353 
1354     cpBuf = malloc (sizeof (char) * (strlen (ccpEndings) + 1));
1355     if (NULL == cpBuf){
1356         free (cpBufSingleEnding);
1357         TRACEIT (2, "CheckForEndingInEndingList Error unable to allocate memory");
1358         return -1;
1359     }
1360     strcpy (cpBuf, ccpEndings);
1361 
1362     cpBuf2 = strtok (cpBuf, " ");
1363     while (NULL != cpBuf2){
1364         if (0 == strcmp (cpBuf2, cpBufSingleEnding)){
1365             iRet = 1;
1366             break;
1367         }
1368         cpBuf2 = strtok (NULL, " ");
1369     }
1370 
1371     free (cpBufSingleEnding);
1372     free (cpBuf);
1373     TRACEIT(10,"CheckForEndingInEndingList		ends");
1374     return iRet;
1375 }
1376 
1377 
ListWidget_MousePressEventCatcher(GtkWidget * widget,GdkEventButton * event,struct ListWidgetCommunicationStruct * commStru)1378 gboolean ListWidget_MousePressEventCatcher (GtkWidget *widget, GdkEventButton *event, struct ListWidgetCommunicationStruct *commStru){
1379     TRACEIT(10,"ListWidget_MousePressEventCatcher		start");
1380 
1381     if (commStru == NULL){
1382         TRACEIT (2, "ListWidget_MousePressEventCatcher      PARAMETER NULL");
1383         return FALSE;
1384     }
1385 
1386     if (commStru->iListPaused != 0){
1387         TRACEIT(10,"ListWidget_MousePressEventCatcher		ends");
1388         return FALSE;
1389     }
1390 
1391     TRACEIT(10,"ListWidget_MousePressEventCatcher		ends");
1392     return TRUE;
1393 }
1394 
1395 
1396 /*
1397 
1398 int VersionCheck_Gtk(){
1399     //int lkd_mj = gtk_major_version;
1400     int lkd_min = gtk_minor_version;
1401     int lkd_mic = gtk_micro_version;
1402     GTK_MAJOR_VERSION,GTK_MINOR_VERSION,GTK_MICRO_VERSION
1403 
1404     if (lkd_min < 10){
1405         return -100;    //Cat'sEyE needs at least version 2.10 //the installed version is too little
1406     }
1407 
1408     if (lkd_min < GTK_MINOR_VERSION){
1409         return -10;     //compiled versions are highter than linked (minor)
1410     }
1411     else if (kd_min = GTK_MINOR_VERSION){
1412         if (lkd_mic < GTK_MICRO_VERSION){
1413             return -1;  //compiled versions are highter than linked (micro)
1414         }
1415         else if (kd_mic = GTK_MICRO_VERSION){
1416             return 0;   //compiled and linked versions are equal
1417         }
1418         else if (kd_mic > GTK_MICRO_VERSION){
1419             return 1;   //linked version is newer than compiled version (micro)
1420         }
1421     }
1422     else if (kd_min > GTK_MINOR_VERSION){
1423         return 10;  //linked version is newer than compiled version (minor)
1424     }
1425 }
1426 */
1427 
1428