File: browser.c

package info (click to toggle)
isomaster 1.3.3-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 1,644 kB
  • ctags: 762
  • sloc: ansic: 11,004; cpp: 273; makefile: 205; sh: 65; python: 11
file content (337 lines) | stat: -rw-r--r-- 11,385 bytes parent folder | download | duplicates (4)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
/******************************* LICENCE **************************************
* Any code in this file may be redistributed or modified under the terms of
* the GNU General Public Licence as published by the Free Software 
* Foundation; version 2 of the licence.
****************************** END LICENCE ***********************************/

/******************************************************************************
* Author:
* Andrew Smith, http://littlesvr.ca/misc/contactandrew.php
*
* Contributors:
* 
******************************************************************************/

#include <gtk/gtk.h>
#include <string.h>
#include <sys/stat.h>
#include <libintl.h>

#include "isomaster.h"

/* this file has thigs shared by the fs and the iso browser */

/* menu-sized pixbufs of a directory and a file */
GdkPixbuf* GBLdirPixbuf;
GdkPixbuf* GBLfilePixbuf;
//~ GdkPixbuf* GBLsymlinkPixbuf;

/* text box for showing the path and name of the current directory on the fs */
GtkWidget* GBLfsCurrentDirField;
/* the view used for the contents of the fs browser */
GtkWidget* GBLfsTreeView;
/* the list store used for the contents of the fs browser */
GtkListStore* GBLfsListStore;
/* slash-terminated, the dir being displayed in the fs browser */
char* GBLfsCurrentDir = NULL;

/* text box for showing the path and name of the current directory on the iso */
GtkWidget* GBLisoCurrentDirField;
/* the view used for the contents of the fs browser */
GtkWidget* GBLisoTreeView;
/* the list store used for the contents of the fs browser */
GtkListStore* GBLisoListStore;
/* slash-terminated, the dir being displayed in the iso browser */
char* GBLisoCurrentDir = NULL;

extern GtkWidget* GBLmainWindow;
extern VolInfo GBLvolInfo;
extern bool GBLisoPaneActive;
extern AppSettings GBLappSettings;

/* connected to the activate signal of a text entry in a dialog */
void acceptDialogCbk(GtkEntry *entry, GtkDialog* dialog)
{
    gtk_dialog_response(dialog, GTK_RESPONSE_ACCEPT);
}

void createDirCbk(GtkButton *button, gpointer onFs)
{
    GtkWidget* dialog;
    GtkWidget* warningDialog;
    int response;
    GtkWidget* textEntry;
    const char* newDirName;
    int rc;
    
    if(!onFs && !GBLisoPaneActive)
    /* asked to create dir on iso but no iso is open */
        return;
    
    dialog = gtk_dialog_new_with_buttons(_("Enter name for new directory"),
                                         GTK_WINDOW(GBLmainWindow),
                                         GTK_DIALOG_DESTROY_WITH_PARENT,
                                         GTK_STOCK_OK,
                                         GTK_RESPONSE_ACCEPT,
                                         GTK_STOCK_CANCEL,
                                         GTK_RESPONSE_REJECT,
                                         NULL);
    
    textEntry = gtk_entry_new();
    gtk_entry_set_width_chars(GTK_ENTRY(textEntry), 40);
    gtk_container_add(GTK_CONTAINER(GTK_DIALOG(dialog)->vbox), textEntry);
    gtk_widget_show(textEntry);
    g_signal_connect(textEntry, "activate", (GCallback)acceptDialogCbk, dialog);
    g_signal_connect(dialog, "close", G_CALLBACK(rejectDialogCbk), NULL);
    
    response = gtk_dialog_run(GTK_DIALOG(dialog));
    
    if(response == GTK_RESPONSE_ACCEPT)
    {
        newDirName = gtk_entry_get_text(GTK_ENTRY(textEntry));
        
        if(onFs)
        {
            char* pathAndName;
            
            pathAndName = malloc(strlen(GBLfsCurrentDir) + strlen(newDirName) + 1);
            if(pathAndName == NULL)
                fatalError("createDirCbk(): malloc(strlen(GBLfsCurrentDir) + "
                           "strlen(newDirName) + 1) failed");
            
            strcpy(pathAndName, GBLfsCurrentDir);
            strcat(pathAndName, newDirName);
            
            rc = mkdir(pathAndName, 0755);
            if(rc == -1)
            {
                warningDialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
                                                       GTK_DIALOG_DESTROY_WITH_PARENT,
                                                       GTK_MESSAGE_ERROR,
                                                       GTK_BUTTONS_CLOSE,
                                                       _("Failed to create directory %s"),
                                                       pathAndName);
                gtk_window_set_modal(GTK_WINDOW(warningDialog), TRUE);
                gtk_dialog_run(GTK_DIALOG(warningDialog));
                gtk_widget_destroy(warningDialog);
                gtk_widget_destroy(dialog);
                return;
            }
            
            free(pathAndName);
            
            refreshFsView();
        }
        else
        /* on iso */
        {
            rc = bk_create_dir(&GBLvolInfo, GBLisoCurrentDir, newDirName);
            if(rc <= 0)
            {
                warningDialog = gtk_message_dialog_new(GTK_WINDOW(GBLmainWindow),
                                                       GTK_DIALOG_DESTROY_WITH_PARENT,
                                                       GTK_MESSAGE_ERROR,
                                                       GTK_BUTTONS_CLOSE,
                                                       _("Failed to create directory %s: '%s'"),
                                                       newDirName,
                                                       bk_get_error_string(rc));
                gtk_window_set_modal(GTK_WINDOW(warningDialog), TRUE);
                gtk_dialog_run(GTK_DIALOG(warningDialog));
                gtk_widget_destroy(warningDialog);
                gtk_widget_destroy(dialog);
                return;
            }
            
            refreshIsoView();
        }
        fflush(NULL);
    }
    
    gtk_widget_destroy(dialog);
}

void formatSize(bk_off_t sizeInt, char* sizeStr, int sizeStrLen)
{
    if(sizeInt > 1073741824)
    /* print gibibytes */
        snprintf(sizeStr, sizeStrLen, "%.1f GB", (double)sizeInt / 1073741824);
    else if(sizeInt > 1048576)
    /* print mebibytes */
        snprintf(sizeStr, sizeStrLen, "%.1f MB", (double)sizeInt / 1048576);
    else if(sizeInt > 1024)
    /* print kibibytes */
        snprintf(sizeStr, sizeStrLen, "%.1f KB", (double)sizeInt / 1024);
    else
    /* print bytes */
        snprintf(sizeStr, sizeStrLen, "%llu B", sizeInt);
    
    sizeStr[sizeStrLen - 1] = '\0';
}

void refreshBothViewsCbk(GtkWidget *widget, GdkEvent *event)
{
    refreshFsView();
    if(GBLisoPaneActive)
        refreshIsoView();
}

/* formats the file size text for displaying */
void sizeCellDataFunc32(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                        GtkTreeModel *model, GtkTreeIter *iter,
                        gpointer data)
{
    unsigned sizeInt;
    unsigned long long sizeLlInt;
    int fileType;
    char sizeStr[20];
    
    gtk_tree_model_get(model, iter, COLUMN_SIZE, &sizeInt, 
                                    COLUMN_HIDDEN_TYPE, &fileType, -1);
    
    sizeLlInt = sizeInt;
    
    if(fileType == FILE_TYPE_DIRECTORY)
    {
        snprintf(sizeStr, sizeof(sizeStr), "dir");
        sizeStr[sizeof(sizeStr) - 1] = '\0';
    }
    else if(fileType == FILE_TYPE_SYMLINK)
    {
        snprintf(sizeStr, sizeof(sizeStr), "link");
        sizeStr[sizeof(sizeStr) - 1] = '\0';
    }
    else
        formatSize(sizeLlInt, sizeStr, sizeof(sizeStr));
    
    g_object_set(renderer, "text", sizeStr, NULL);
}

/* formats the file size text for displaying */
void sizeCellDataFunc64(GtkTreeViewColumn *col, GtkCellRenderer *renderer,
                        GtkTreeModel *model, GtkTreeIter *iter,
                        gpointer data)
{
    unsigned long long sizeInt;
    int fileType;
    char sizeStr[20];
    
    gtk_tree_model_get(model, iter, COLUMN_SIZE, &sizeInt, 
                                    COLUMN_HIDDEN_TYPE, &fileType, -1);
    
    if(fileType == FILE_TYPE_DIRECTORY)
    {
        snprintf(sizeStr, sizeof(sizeStr), "dir");
        sizeStr[sizeof(sizeStr) - 1] = '\0';
    }
    else if(fileType == FILE_TYPE_SYMLINK)
    {
        snprintf(sizeStr, sizeof(sizeStr), "link");
        sizeStr[sizeof(sizeStr) - 1] = '\0';
    }
    else
        formatSize(sizeInt, sizeStr, sizeof(sizeStr));
    
    g_object_set(renderer, "text", sizeStr, NULL);
}

gint sortByName(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata)
{
    if(GBLappSettings.sortDirectoriesFirst)
    /* directories before files */
    {
        int aFileType;
        int bFileType;
        gint unused;
        GtkSortType order;
        
        gtk_tree_model_get(model, a, COLUMN_HIDDEN_TYPE, &aFileType, -1);
        gtk_tree_model_get(model, b, COLUMN_HIDDEN_TYPE, &bFileType, -1);
        
        /* have to make sure directories come first regardless of sort order, 
        * that's why all the fancyness in the rest of this block */
        gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(model), &unused, &order);
        
        if(aFileType == FILE_TYPE_DIRECTORY && bFileType != FILE_TYPE_DIRECTORY)
        {
            if(order == GTK_SORT_ASCENDING)
                return -1;
            else
                return 1;
        }
        else if(aFileType != FILE_TYPE_DIRECTORY && bFileType == FILE_TYPE_DIRECTORY)
        {
            if(order == GTK_SORT_ASCENDING)
                return 1;
            else
                return -1;
        }
    }
    
    char* aName;
    char* bName;
    gint toReturn;
    
    gtk_tree_model_get(model, a, COLUMN_FILENAME, &aName, -1);
    gtk_tree_model_get(model, b, COLUMN_FILENAME, &bName, -1);
    
    if(GBLappSettings.caseSensitiveSort)
        toReturn = strcmp(aName, bName);
    else
        toReturn = strcasecmp(aName, bName);
    
    g_free(aName);
    g_free(bName);
    
    return toReturn;
}

gint sortBySize(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata)
{
    if(GBLappSettings.sortDirectoriesFirst)
    /* directories before files */
    {
        int aFileType;
        int bFileType;
        gint unused;
        GtkSortType order;
        
        gtk_tree_model_get(model, a, COLUMN_HIDDEN_TYPE, &aFileType, -1);
        gtk_tree_model_get(model, b, COLUMN_HIDDEN_TYPE, &bFileType, -1);
        
        /* have to make sure directories come first regardless of sort order, 
        * that's why all the fancyness below */
        gtk_tree_sortable_get_sort_column_id(GTK_TREE_SORTABLE(model), &unused, &order);
        
        if(aFileType == FILE_TYPE_DIRECTORY && bFileType != FILE_TYPE_DIRECTORY)
        {
            if(order == GTK_SORT_ASCENDING)
                return -1;
            else
                return 1;
        }
        else if(aFileType != FILE_TYPE_DIRECTORY && bFileType == FILE_TYPE_DIRECTORY)
        {
            if(order == GTK_SORT_ASCENDING)
                return 1;
            else
                return -1;
        }
    }
    
    guint64 aSize;
    guint64 bSize;
    
    gtk_tree_model_get(model, a, COLUMN_SIZE, &aSize, -1);
    gtk_tree_model_get(model, b, COLUMN_SIZE, &bSize, -1);
    
    if(aSize < bSize)
        return -1;
    else
        return 1;
}

/* this function exists to deal with the gtk stupidity that sorting can't be disabled */
gint sortVoid(GtkTreeModel *model, GtkTreeIter *a, GtkTreeIter *b, gpointer userdata)
{
    return 0;
}