File: dir_window_handler.c

package info (click to toggle)
ripperx 2.7.2-3
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 2,256 kB
  • ctags: 609
  • sloc: sh: 10,579; ansic: 6,939; makefile: 145
file content (134 lines) | stat: -rw-r--r-- 4,403 bytes parent folder | download | duplicates (3)
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
#ifdef HAVE_CONFIG_H
#  include "config.h"
#endif

#include <glib.h>
#include <glib/gi18n.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>
#include <gtk/gtk.h>

#include "misc_utils.h"
#include "interface_common.h"

#include "dir_window_handler.h"

void dw_cancel_clicked( GtkWidget *widget, gpointer callback_data );
void dw_ok_clicked( GtkWidget *widget, gpointer callback_data );


void dw_cancel_clicked( GtkWidget *widget, gpointer callback_data )
{
	dir_window_handler( DW_CANCEL, NULL );
}

void dw_ok_clicked( GtkWidget *widget, gpointer callback_data )
{
	dir_window_handler( DW_OK, NULL );
}

char *dir_window_handler( int ops, char *cur_dir )
{
	static GtkWidget * filew;
	static int id;
	static char buf[ MAX_FILE_PATH_LENGTH ];
	static char *saved_cur_dir;

	switch ( ops ) {
	case WIDGET_CREATE : {
			struct stat st;
			char check_dir[ MAX_FILE_PATH_LENGTH + 1];
			char *checked_dir;
			saved_cur_dir = cur_dir;
			checked_dir = check_dir;
			strncpy(check_dir, cur_dir, MAX_FILE_PATH_LENGTH );
			
			/* Add a final / to input directory string if missing and input string is only a
			directory (i.e. no filename); but pass through any other strings.  This allows
			GtkFileSelection widget to show proper directory level upon entering. */
			if ( ( lstat( checked_dir, &st ) >= 0 ) && ( S_ISDIR( st.st_mode ) )) {
				if( ( check_dir[ strlen( checked_dir ) - 1 ] != '/' )
						&& ( strlen(checked_dir) > 0 )
						&& ( strlen(checked_dir) < MAX_FILE_PATH_LENGTH ) ) {
					checked_dir = strcat( check_dir, "/" );
				}
			}

			filew = gtk_file_selection_new(_("Select a directory"));
			gtk_window_set_position ( GTK_WINDOW ( filew ), GTK_WIN_POS_MOUSE );
			gtk_file_selection_show_fileop_buttons ( GTK_FILE_SELECTION ( filew ) );

			id = g_signal_connect( G_OBJECT( filew ), "destroy",
			                         G_CALLBACK( dw_cancel_clicked ),
			                         NULL );
			g_signal_connect( G_OBJECT( GTK_FILE_SELECTION( filew ) ->ok_button ),
			                    "clicked",
			                    G_CALLBACK( dw_ok_clicked ), NULL );
			g_signal_connect( G_OBJECT( GTK_FILE_SELECTION( filew ) ->cancel_button ),
			                    "clicked",
			                    G_CALLBACK( dw_cancel_clicked ),
			                    NULL );
			gtk_file_selection_set_filename( GTK_FILE_SELECTION( filew ), checked_dir );
			gtk_widget_hide( GTK_WIDGET( GTK_FILE_SELECTION( filew ) ->fileop_del_file ));
			gtk_widget_hide( GTK_WIDGET( GTK_FILE_SELECTION( filew ) ->fileop_ren_file ));
			gtk_widget_set_sensitive( GTK_WIDGET( GTK_FILE_SELECTION( filew ) ->file_list ),
			                    FALSE );
			gtk_widget_show( filew );
			gtk_main();
			g_signal_handler_disconnect( G_OBJECT( filew ), id );
			strncpy( buf,
			         gtk_file_selection_get_filename( GTK_FILE_SELECTION( filew ) ),
			         sizeof( buf ) );
			gtk_widget_destroy( filew );
			return buf;
		}

	case DW_OK : {
			struct stat st;
			char *temp;

			temp = gtk_file_selection_get_filename( GTK_FILE_SELECTION( filew ) );
			if ( lstat( temp, &st ) < 0 ) {
				err_handler( INVALID_FILE_SELECTION_ERR, NULL );
				return NULL;
			}
			if ( !S_ISDIR( st.st_mode ) ) {
				gtk_file_selection_set_filename( GTK_FILE_SELECTION( filew ),
				                                 file_path_without_name( temp ) );
				temp = gtk_file_selection_get_filename( GTK_FILE_SELECTION( filew ) );
			}
			/* remove final directory ../ from directory string if present */
			if( strlen( temp ) > 3 ) {
				if( ( temp[ strlen( temp ) - 1 ] == '/' ) &&
						( temp[ strlen( temp ) - 2 ] == '.' ) &&
						( temp[ strlen( temp ) - 3 ] == '.' ) &&
						( temp[ strlen( temp ) - 4 ] == '/' ) ) {
					temp[ strlen( temp ) - 3 ] = '\0';
					gtk_file_selection_set_filename( GTK_FILE_SELECTION( filew ),
						temp );
				}
			}
			/* remove final directory ./ from directory string if present */
			if ( strlen( temp ) > 2 ) {
				if( ( temp[ strlen( temp ) - 1 ] == '/' ) &&
						( temp[ strlen( temp ) - 2 ] == '.' ) &&
						( temp[ strlen( temp ) - 3 ] == '/' ) ) {
					temp[ strlen( temp ) - 2 ] = '\0';
					gtk_file_selection_set_filename( GTK_FILE_SELECTION( filew ),
						temp );
				}
			}
			gtk_main_quit();
			return NULL;
		}

	case DW_CANCEL :
		gtk_file_selection_set_filename( GTK_FILE_SELECTION( filew ), saved_cur_dir );
		gtk_main_quit();
		return NULL;
	}
	return NULL;
}