Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/browser/GeckoDragDropHooks.cpp
blob: 995ee87f67d93d7b007ac4d640ae9f5ecccbe180 (plain)
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
#include <config.h>

#include <sys/time.h>
#include <time.h>

#include <glib.h>
#include <nsStringAPI.h>
#include <nsCOMPtr.h>
#include <nsITransferable.h>
#include <nsISupportsPrimitives.h>
#include <nsIDOMEventTarget.h>
#include <nsComponentManagerUtils.h>
#include <nsServiceManagerUtils.h>
#include <nsIInterfaceRequestorUtils.h>
#include <nsIDOMMouseEvent.h>
#include <nsIMIMEService.h>

#include "GeckoDragDropHooks.h"
#include "GeckoDocumentObject.h"

#define TEXT_URI_LIST   "text/uri-list"
#define TEXT_X_MOZ_URL  "text/x-moz-url"
#define FILE_LOCALHOST  "file://"

//*****************************************************************************
// UriListDataProvider
//*****************************************************************************

class UriListDataProvider : public nsIFlavorDataProvider
{
public:
    UriListDataProvider(GeckoDocumentObject *mDocumentObject);
    virtual ~UriListDataProvider();
    NS_DECL_ISUPPORTS
    NS_DECL_NSIFLAVORDATAPROVIDER
private:
    GeckoDocumentObject *mDocumentObject;
    nsCString            mFilePath;
};

//*****************************************************************************

NS_IMPL_ISUPPORTS1(UriListDataProvider, nsIFlavorDataProvider)                                                   

UriListDataProvider::UriListDataProvider(GeckoDocumentObject *documentObject)
    :   mDocumentObject(documentObject)
{
}

UriListDataProvider::~UriListDataProvider()
{
    if(mFilePath.Length()) {
        remove(mFilePath.get());
    }
    
    delete mDocumentObject;
}

NS_IMETHODIMP
UriListDataProvider::GetFlavorData(nsITransferable *aTransferable,
                                   const char *aFlavor, nsISupports **aData,
                                   PRUint32 *aDataLen)
{
    NS_ENSURE_ARG_POINTER(aData && aDataLen);

    nsresult rv = NS_ERROR_NOT_IMPLEMENTED;
    char *image_name;
    char *mime_type;
    char *file_ext;
    nsCString mime_ext;
    timeval timestamp;

    *aData = nsnull;
    *aDataLen = 0;
    
    if(g_ascii_strcasecmp(aFlavor, TEXT_URI_LIST) != 0) {
        return rv;
    }

    gettimeofday(&timestamp, NULL);
    
    mFilePath.Append(g_get_tmp_dir());
    mFilePath.Append("/");
    mFilePath.AppendInt(timestamp.tv_sec);
    mFilePath.AppendInt(timestamp.tv_usec);

    image_name = mDocumentObject->GetImageName();
    file_ext = strrchr(image_name, '.');
    mime_type = mDocumentObject->GetImageMimeType();
    
    nsCOMPtr<nsIMIMEService> mimeService(do_GetService("@mozilla.org/mime;1",
                                                       &rv));
    NS_ENSURE_SUCCESS(rv, rv);
    rv = mimeService->GetPrimaryExtension(nsCString(mime_type),
                                          EmptyCString(),
                                          mime_ext);
    NS_ENSURE_SUCCESS(rv, rv);

    if(!file_ext) {
        mFilePath.Append(image_name);
        mFilePath.Append(".");
        mFilePath.Append(mime_ext);    
    } else if(strcmp(file_ext, mime_ext.get())) {
        image_name[strlen(file_ext)] = 0;
        mFilePath.Append(image_name);
        mFilePath.Append(".");
        mFilePath.Append(mime_ext);
    } else {
        mFilePath.Append(image_name);
    }

    g_free(mime_type);
    g_free(image_name);

    if(!mDocumentObject->SaveImage(mFilePath.get())) {
        return NS_ERROR_FAILURE;
    }

    nsCString localURI(FILE_LOCALHOST);
    localURI.Append(mFilePath);

    nsString localURI16;
    NS_CStringToUTF16(localURI, NS_CSTRING_ENCODING_UTF8, localURI16);

    nsCOMPtr<nsISupportsString> localURIData(do_CreateInstance(
                "@mozilla.org/supports-string;1", &rv));
    if(NS_FAILED(rv)) return rv;

    rv = localURIData->SetData(localURI16);
    if(NS_FAILED(rv)) return rv;

    CallQueryInterface(localURIData, aData);
    *aDataLen = sizeof(nsISupportsString*);

    // FIXME: Why do we need this? Is there a leak in mozilla?
    this->Release();

    return rv;
}

//*****************************************************************************
// GeckoDragDropHooks
//*****************************************************************************

NS_IMPL_ISUPPORTS1(GeckoDragDropHooks, nsIClipboardDragDropHooks)

GeckoDragDropHooks::GeckoDragDropHooks(SugarBrowser *browser)
    :   mBrowser(browser)
{
}

GeckoDragDropHooks::~GeckoDragDropHooks()
{
}

NS_IMETHODIMP
GeckoDragDropHooks::AllowStartDrag(nsIDOMEvent *event, PRBool *_retval)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
GeckoDragDropHooks::AllowDrop(nsIDOMEvent *event, nsIDragSession *session,
                              PRBool *_retval)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
GeckoDragDropHooks::OnCopyOrDrag(nsIDOMEvent *aEvent, nsITransferable *trans,
                                 PRBool *_retval)
{
    nsresult rv;

    *_retval = true;

    nsCOMPtr<nsIDOMMouseEvent> mouseEvent;
    mouseEvent = do_QueryInterface(aEvent, &rv);
    if(NS_FAILED(rv)) return rv;

    nsCOMPtr<nsIDOMEventTarget> eventTarget;
    rv = mouseEvent->GetTarget(getter_AddRefs(eventTarget));
    if(NS_FAILED(rv)) return rv;

    nsCOMPtr<nsIDOMNode> targetNode;
    targetNode = do_QueryInterface(eventTarget, &rv);
    if(NS_FAILED(rv)) return rv;

    GeckoDocumentObject *documentObject = new GeckoDocumentObject(mBrowser,
                                                                  targetNode);
    if(documentObject->IsImage()) {
        rv = trans->RemoveDataFlavor(TEXT_X_MOZ_URL);
        if(NS_FAILED(rv)) return rv;

        rv = trans->AddDataFlavor(TEXT_URI_LIST);
        if(NS_FAILED(rv)) return rv;

        UriListDataProvider *rawPtr = new UriListDataProvider(documentObject);
        nsCOMPtr<nsISupports> dataProvider(do_QueryInterface(rawPtr, &rv));
        if(NS_FAILED(rv)) return rv;

        rv = trans->SetTransferData(TEXT_URI_LIST, dataProvider, 0);
        if(NS_FAILED(rv)) return rv;
    }

    return rv;
}

NS_IMETHODIMP
GeckoDragDropHooks::OnPasteOrDrop(nsIDOMEvent *event, nsITransferable *trans,
                                  PRBool *_retval)
{
    return NS_ERROR_NOT_IMPLEMENTED;
}