~ubuntu-branches/debian/sid/gmtp/sid

« back to all changes in this revision

Viewing changes to src/dnd.c

  • Committer: Bazaar Package Importer
  • Author(s): Alessio Treglia
  • Date: 2011-01-24 17:21:38 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110124172138-30ssabr3ki093ji8
Tags: 0.8-1
* New upstream release.
* Refresh patches.
* Update debian/copyright.
* Add patch to improve the Italian translation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
*
 
3
*   File: dnd.c
 
4
*   
 
5
*   Copyright (C) 2009-2011 Darran Kartaschew
 
6
*
 
7
*   This file is part of the gMTP package.
 
8
*
 
9
*   gMTP is free software; you can redistribute it and/or modify
 
10
*   it under the terms of the BSD License as included within the
 
11
*   file 'COPYING' located in the root directory
 
12
*
 
13
*/
 
14
/*
 
15
        This file contains all the Drag and Drop Functionality for gMTP
 
16
 */
 
17
 
 
18
#include "config.h"
 
19
 
 
20
#include <glib.h>
 
21
#include <gtk/gtk.h>
 
22
#include <glib/gprintf.h>
 
23
#include <glib/gi18n.h>
 
24
#include <gconf/gconf.h>
 
25
#include <gconf/gconf-client.h>
 
26
#include <sys/types.h>
 
27
#include <libgen.h>
 
28
#include <string.h>
 
29
#include <id3tag.h>
 
30
#include <libmtp.h>
 
31
 
 
32
#include "main.h"
 
33
#include "interface.h"
 
34
#include "callbacks.h"
 
35
#include "mtp.h"
 
36
#include "prefs.h"
 
37
#include "dnd.h"
 
38
 
 
39
const GtkTargetEntry _gmtp_drop_types[] =
 
40
{
 
41
        {"text/plain",                  0,      GMTP_DROP_PLAINTEXT},
 
42
        {"text/uri-list",               0,      GMTP_DROP_URLENCODED},
 
43
        {"STRING",              0,      GMTP_DROP_STRING}
 
44
};
 
45
 
 
46
void gmtp_drag_data_received(GtkWidget * widget,
 
47
                                       GdkDragContext * context,
 
48
                                       gint x,
 
49
                                       gint y,
 
50
                                       GtkSelectionData * selection_data,
 
51
                                       guint info,
 
52
                                       guint time,
 
53
                                       gpointer user_data)
 
54
{
 
55
        if (selection_data->data)
 
56
        {
 
57
        
 
58
        GSList* files;
 
59
        //g_printf("Selection->data = %s\n", selection_data->data);
 
60
 
 
61
        files = getFilesListURI((gchar *)selection_data->data);
 
62
        if(files != NULL){
 
63
            //g_print("Do each file\n");
 
64
            g_slist_foreach(files, (GFunc)__filesAdd, NULL);
 
65
        }
 
66
        // Now clear the GList;
 
67
        g_slist_foreach(files, (GFunc)g_free, NULL);
 
68
        g_slist_free(files);
 
69
        // Now do a device rescan to see the new files.
 
70
        deviceRescan();
 
71
        deviceoverwriteop = MTP_ASK;
 
72
        }
 
73
}
 
74
 
 
75
GSList* getFilesListURI(gchar* rawdata){
 
76
    // The data is just the data in string form
 
77
    // Files are in the URI form of file:///filename\n so just look for those,
 
78
    // and if found see if a folder or not?
 
79
    // Then create a slist of those and use filesAdd() to add them in .
 
80
    GSList* filelist;
 
81
    gchar* tmpstring;
 
82
    gchar *fullpath;
 
83
    gchar *filepath;
 
84
    gchar *token;
 
85
    //struct stat statbuf;
 
86
    const char delimit[]="\n\r";
 
87
 
 
88
    filelist = NULL;
 
89
 
 
90
    fullpath = g_strdup(rawdata);
 
91
 
 
92
    token = strtok (fullpath, delimit);
 
93
    while((token != NULL)){
 
94
        // Now test to see if we have it here...
 
95
        filepath = g_strdup(token);
 
96
        // See if we have a local file URI, otherwise discard.
 
97
        if(!g_ascii_strncasecmp(filepath, "file://", 7)){
 
98
            tmpstring = g_filename_from_uri(filepath, NULL, NULL);
 
99
            if (g_file_test(tmpstring, G_FILE_TEST_IS_REGULAR ) == TRUE){
 
100
                filelist = g_slist_append(filelist, g_strdup(tmpstring));
 
101
            } else {
 
102
                //g_printf("Parsing folder \"%s\" via DnD interface\n", tmpstring);
 
103
                addFilesinFolder(tmpstring);
 
104
            }
 
105
            g_free(tmpstring);
 
106
        }
 
107
        token = strtok(NULL, delimit);
 
108
        g_free(filepath);
 
109
    }
 
110
    g_free(fullpath);
 
111
    return filelist;
 
112
}
 
113
 
 
114
void addFilesinFolder(gchar* foldername){
 
115
    // foldername is the name of the folder as the absolute path with leading /
 
116
    // We save the currentFolderID, create a new folder on the device,
 
117
    // and set currentFolderID to the new folders ID.
 
118
    // Then scan the folder (on the filesystem) adding in files as needed.
 
119
    // Found folders are always added first, so files are copied from
 
120
    // the deepest level of the folder hierarchy first as well, and we
 
121
    // work our way back down towards to the initial folder that was
 
122
    // dragged in.
 
123
    // Lastly we restore the currentFolderID back to what it was.
 
124
    GDir *fileImageDir;
 
125
    GSList* filelist;
 
126
    const gchar *filename;
 
127
    gchar* relative_foldername;
 
128
    gchar *tmpstring;
 
129
    uint32_t oldFolderID;
 
130
 
 
131
    filelist = NULL;
 
132
    // Save our current working folder.
 
133
    oldFolderID = currentFolderID;
 
134
    // Get just the folder name, as we are given a full absolute path.
 
135
    relative_foldername = basename(foldername);
 
136
    if(relative_foldername != NULL){
 
137
        //g_printf("New MTP Folder = %s\n", relative_foldername);
 
138
        // Add our folder to the mtp device and set our new current working folder ID.
 
139
        currentFolderID = folderAdd(relative_foldername);
 
140
    }
 
141
 
 
142
    // Start scanning the folder on the filesystem for our new files/folders.
 
143
    fileImageDir = g_dir_open(foldername, 0, NULL);
 
144
    // Now parse that directory looking for JPEG/PNG files (based on settings).
 
145
    // If we find one, we create a new GString and add it to the list.
 
146
    if(fileImageDir != NULL){
 
147
        filename = g_dir_read_name(fileImageDir);
 
148
        while(filename != NULL){
 
149
            // See if a file or a folder?
 
150
            tmpstring = g_strconcat(foldername, "/", filename, NULL);
 
151
            if (g_file_test(tmpstring, G_FILE_TEST_IS_REGULAR ) == TRUE){
 
152
                // We have a regular file. So add it to the list.
 
153
                //g_printf("File = %s\n", tmpstring);
 
154
                filelist = g_slist_append(filelist, g_strdup(tmpstring));
 
155
            } else {
 
156
                if (g_file_test(tmpstring, G_FILE_TEST_IS_DIR ) == TRUE){
 
157
                    // We have another folder so recursively call ourselves...
 
158
                    //g_printf("Folder = %s\n", tmpstring);
 
159
                    addFilesinFolder( tmpstring);
 
160
                }
 
161
            }
 
162
            filename = g_dir_read_name(fileImageDir);
 
163
            g_free(tmpstring);
 
164
        }
 
165
    }
 
166
    // Upload our given files in the current selected folder.
 
167
    if(filelist != NULL){
 
168
        //g_print("Do each file\n");
 
169
        g_slist_foreach(filelist, (GFunc)__filesAdd, NULL);
 
170
    }
 
171
    // Now clear the GList;
 
172
    g_slist_foreach(filelist, (GFunc)g_free, NULL);
 
173
    g_slist_free(filelist);
 
174
 
 
175
    if(fileImageDir != NULL)
 
176
        g_dir_close(fileImageDir);
 
177
    // Restore our current working folder.
 
178
    currentFolderID = oldFolderID;
 
179
}