summaryrefslogtreecommitdiff
path: root/app/wlib/gtklib/filesel.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/wlib/gtklib/filesel.c')
-rw-r--r--app/wlib/gtklib/filesel.c64
1 files changed, 51 insertions, 13 deletions
diff --git a/app/wlib/gtklib/filesel.c b/app/wlib/gtklib/filesel.c
index 4c737ae..4b92389 100644
--- a/app/wlib/gtklib/filesel.c
+++ b/app/wlib/gtklib/filesel.c
@@ -31,16 +31,19 @@
#include "gtkint.h"
#include "i18n.h"
+#define MAX_ALLOWEDFILTERS 10
+
struct wFilSel_t {
GtkWidget * window;
wFilSelCallBack_p action;
void * data;
int pattCount;
- GtkFileFilter *filter[ 10 ];
+ GtkFileFilter *filter[ MAX_ALLOWEDFILTERS ];
wFilSelMode_e mode;
int opt;
const char * title;
wWin_p parent;
+ char *defaultExtension;
};
@@ -90,11 +93,15 @@ struct wFilSel_t * wFilSelCreate(
count = 0;
// names and patterns are separated by |
cp = strtok( cp, "|" );
- while ( cp && count < 9 ) {
+ while ( cp && count < (MAX_ALLOWEDFILTERS - 1)) {
fs->filter[ count ] = gtk_file_filter_new ();
gtk_file_filter_set_name ( fs->filter[ count ], cp );
cp = strtok( NULL, "|" );
gtk_file_filter_add_pattern (fs->filter[ count ], cp );
+ // the first pattern is considered to match the default extension
+ if( count == 0 ) {
+ fs->defaultExtension = strdup( cp );
+ }
cp = strtok( NULL, "|" );
count++;
}
@@ -121,9 +128,15 @@ struct wFilSel_t * wFilSelCreate(
int wFilSelect( struct wFilSel_t * fs, const char * dirName )
{
char name[1024];
- char *fileName;
- const char *base;
+ char *host;
+ char *file;
+ char *namePart;
int i;
+ GSList *fileNameList;
+ GError *err = NULL;
+ GtkFileFilter *activeFilter;
+
+ char **fileNames;
char * cp;
if (fs->window == NULL) {
@@ -137,6 +150,10 @@ int wFilSelect( struct wFilSel_t * fs, const char * dirName )
// get confirmation before overwritting an existing file
gtk_file_chooser_set_do_overwrite_confirmation( GTK_FILE_CHOOSER(fs->window), TRUE );
+ // allow selecting multiple files
+ if( fs->opt & FS_MULTIPLEFILES ) {
+ gtk_file_chooser_set_select_multiple ( GTK_FILE_CHOOSER(fs->window), TRUE);
+ }
// add the file filters to the dialog box
if( fs->pattCount ) {
for( i = 0; i <= fs->pattCount; i++ ) {
@@ -156,17 +173,38 @@ int wFilSelect( struct wFilSel_t * fs, const char * dirName )
gtk_file_chooser_set_current_name( GTK_FILE_CHOOSER(fs->window), name );
if( gtk_dialog_run( GTK_DIALOG( fs->window )) == GTK_RESPONSE_ACCEPT ) {
- fileName = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER(fs->window) );
- if (fs->data)
- strcpy( fs->data, fileName );
+
+ fileNameList = gtk_file_chooser_get_uris( GTK_FILE_CHOOSER(fs->window) );
+ fileNames = calloc( sizeof(char *), g_slist_length (fileNameList) );
+
+ for (i=0; i < g_slist_length (fileNameList); i++ ) {
+ file = g_filename_from_uri( g_slist_nth_data( fileNameList, i ), &host, &err );
+
+ // check for presence of file extension
+ // jump behind tha last directory delimiter
+ namePart = strrchr( file, '/' ) + 1;
+ // is there a dot in the last part, yes->extension present
+ if( !strchr( namePart, '.' ) ){
+ // make room for the extension
+ file = g_realloc( file, strlen(file)+strlen(fs->defaultExtension));
+ strcat( file, fs->defaultExtension + 1 );
+ }
+ fileNames[ i ] = file;
+ g_free( g_slist_nth_data ( fileNameList, i));
+ }
+
+ if (fs->data)
+ strcpy( fs->data, fileNames[ 0 ] );
+
if (fs->action) {
- base = strrchr( fileName, '/' );
- if (base==0) {
- fprintf(stderr,"no / in %s\n", fileName );
- return 1;
- }
- fs->action( fileName, base+1, fs->data );
+ fs->action( g_slist_length(fileNameList), fileNames, fs->data );
+ }
+
+ for(i=0; i < g_slist_length(fileNameList); i++) {
+ g_free( fileNames[ i ]);
}
+ free( fileNames );
+ g_slist_free (fileNameList);
}
gtk_widget_hide( GTK_WIDGET( fs->window ));