-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPasteOMaticView.cpp
333 lines (272 loc) · 7.62 KB
/
PasteOMaticView.cpp
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
/*
* Copyright 2006-2010, Haiku, Inc. All Rights Reserved.
* Distributed under the terms of the MIT License.
*
* Author:
* Andras Sevcsik <[email protected]>
*/
#include "defines.h"
#include "PasteOMatic.h"
#include "PasteOMaticView.h"
#include "PasteOMaticPaster.h"
#include "PasterPastebinCa.h"
#include <Application.h>
#include <Bitmap.h>
#include <File.h>
#include <fs_attr.h>
#include "IconUtils.h"
#include <Message.h>
#include <Messenger.h>
#include <Node.h>
#include <Resources.h>
#include <String.h>
#include <TranslationUtils.h>
#include <View.h>
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
PasteOMaticView::PasteOMaticView()
: BView(BRect(0, 0, 0, 0), "fView", B_FOLLOW_LEFT | B_FOLLOW_V_CENTER,
B_WILL_DRAW)
{
fBitmapDefault = NULL;
fBitmapSuccess = NULL;
fBitmapFail = NULL;
fPanel = NULL;
fSettings = NULL;
fProgress = -1;
fConfig = new BMessage();
fConfigFile = new BFile(CONFIG_PATH, B_READ_WRITE | B_CREATE_FILE);
if (fConfig->Unflatten(fConfigFile) != B_OK)
{
cout << "No config, creating one\n";
fConfig->AddInt16("size", 48);
fConfig->AddBool("deskbar", false);
fConfig->AddBool("autoclip", true);
fConfig->Flatten(fConfigFile);
}
fConfig->FindInt16("size", &fSize);
ResizeTo(fSize, fSize);
SetDrawingMode(B_OP_ALPHA);
SetBlendingMode(B_PIXEL_ALPHA, B_ALPHA_COMPOSITE);
SetViewColor(ui_color(B_PANEL_BACKGROUND_COLOR));
//fDragger = new BDragger(BRect(0, 0, 48, 48), this);
//AddChild(fDragger);
_SetSize(fSize);
_SetDefault();
}
PasteOMaticView::~PasteOMaticView()
{
delete fBitmapDefault;
delete fBitmapSuccess;
delete fBitmapFail;
}
status_t PasteOMaticView::Archive(BMessage *archive, bool deep)
{
cout << "Archive()\n";
archive->AddString("add_on", SIGNATURE);
return B_OK;
}
BArchivable* PasteOMaticView::Instantiate(BMessage *archive)
{
cout << "Instantiate()\n";
if (!validate_instantiation(archive, "PasteOMaticView"))
{
return NULL;
}
return new PasteOMaticView;
}
void PasteOMaticView::_SetDefault()
{
fBitmap = fBitmapDefault;
Invalidate();
}
void PasteOMaticView::_SetSuccess()
{
fBitmap = fBitmapSuccess;
fProgress = -1;
Invalidate();
}
void PasteOMaticView::_SetFail()
{
fBitmap = fBitmapFail;
fProgress = -1;
Invalidate();
}
void PasteOMaticView::_SetWorking()
{
fBitmap = fBitmapWorking;
Invalidate();
}
void PasteOMaticView::_SetProgress(BMessage *message)
{
int8 progress;
if (message->FindInt8("percentage", &progress) == B_OK)
fProgress = progress;
else fProgress = 0;
Invalidate();
}
void PasteOMaticView::Draw(BRect rect)
{
SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR));
FillRect(BRect(0, 0, 48, 48));
DrawBitmap(fBitmap, BPoint(0, 0));
if (fProgress != -1)
{
SetHighColor(ui_color(B_PANEL_BACKGROUND_COLOR).red,
ui_color(B_PANEL_BACKGROUND_COLOR).green,
ui_color(B_PANEL_BACKGROUND_COLOR).blue,
192);
FillRect(BRect(0, 0, fSize, fSize - fSize * (fProgress / 100.0)));
}
}
void PasteOMaticView::MessageReceived(BMessage *message)
{
entry_ref ref;
cout << "View recieved message: \n";
message->PrintToStream();
switch(message->what)
{
case MESSAGE_OPEN:
_ShowPanel();
break;
case MESSAGE_SETTINGS:
_ShowSettings();
break;
case MESSAGE_FROM_OPEN_PANEL:
case MESSAGE_FROM_TRACKER_DND:
if (message->FindRef("refs", &ref) == B_OK)
_StartPaste(ref);
break;
case MESSAGE_PASTE_PROGRESS:
_SetWorking();
if (Looper()) Looper()->PostMessage(message);
_SetProgress(message);
break;
case MESSAGE_PASTE_FAIL:
_SetFail();
if (Looper()) Looper()->PostMessage(message);
break;
case MESSAGE_PASTE_SUCCESS:
_SetSuccess();
if (Looper()) Looper()->PostMessage(message);
break;
default:
BView::MessageReceived(message);
}
}
PasteOMaticPaster *PasteOMaticView::_FindPaster(char *type, size_t size)
{
PasteOMaticPaster *paster;
for (int i = 0; i < 1; i++)
{
switch (i) /* There must be a nicer way to do this... */
{
case 0:
paster = static_cast<PasteOMaticPaster *> (new PasterPastebinCa(this));
break;
}
if (paster->Check(type, size))
{
cout << "Paster will be " << i << endl;
break;
}
else
{
delete paster;
paster = NULL;
}
}
return paster;
}
void PasteOMaticView::_SetSize(uint8 size)
{
if (size <= 16)
{
fSize = 16;
_SetSmall();
}
else
{
fSize = size;
_SetLarge();
}
}
void PasteOMaticView::_SetSmall()
{
if (fBitmapDefault) delete fBitmapDefault;
if (fBitmapSuccess) delete fBitmapSuccess;
if (fBitmapWorking) delete fBitmapWorking;
if (fBitmapFail) delete fBitmapFail;
fBitmapDefault = BTranslationUtils::GetBitmap(B_RAW_TYPE, "icon_small");
fBitmapSuccess = BTranslationUtils::GetBitmap(B_RAW_TYPE, "icon_small_success");
fBitmapWorking = BTranslationUtils::GetBitmap(B_RAW_TYPE, "icon_small_working");
fBitmapFail = BTranslationUtils::GetBitmap(B_RAW_TYPE, "icon_small_fail");
Invalidate();
}
void PasteOMaticView::_SetLarge()
{
uint8 *hvif;
size_t size;
BResources *resources = BApplication::AppResources();
if (fBitmapDefault) delete fBitmapDefault;
if (fBitmapSuccess) delete fBitmapSuccess;
if (fBitmapWorking) delete fBitmapWorking;
if (fBitmapFail) delete fBitmapFail;
fBitmapDefault = new BBitmap(BRect(0, 0, fSize, fSize), B_RGBA32);
fBitmapSuccess = new BBitmap(BRect(0, 0, fSize, fSize), B_RGBA32);
fBitmapWorking = new BBitmap(BRect(0, 0, fSize, fSize), B_RGBA32);
fBitmapFail = new BBitmap(BRect(0, 0, fSize, fSize), B_RGBA32);
hvif = (uint8 *)resources->LoadResource(B_VECTOR_ICON_TYPE, "BEOS:ICON", &size);
if (hvif) BIconUtils::GetVectorIcon(hvif, size, fBitmapDefault);
hvif = (uint8 *)resources->LoadResource(B_VECTOR_ICON_TYPE, "icon_success", &size);
if (hvif) BIconUtils::GetVectorIcon(hvif, size, fBitmapSuccess);
hvif = (uint8 *)resources->LoadResource(B_VECTOR_ICON_TYPE, "icon_working", &size);
if (hvif) BIconUtils::GetVectorIcon(hvif, size, fBitmapWorking);
hvif = (uint8 *)resources->LoadResource(B_VECTOR_ICON_TYPE, "icon_fail", &size);
if (hvif) BIconUtils::GetVectorIcon(hvif, size, fBitmapFail);
Invalidate();
}
void PasteOMaticView::_ShowPanel()
{
if (!fPanel) fPanel = new BFilePanel(B_OPEN_PANEL, new BMessenger(this), NULL, 0, false, new BMessage(MESSAGE_FROM_OPEN_PANEL));
fPanel->Show();
}
void PasteOMaticView::_ShowSettings()
{
if (!fSettings) fSettings = new PasteOMaticSettingsWindow(new BMessenger(this));
fSettings->Show();
}
void PasteOMaticView::_StartPaste(void *data, size_t size)
{
cout << "To be implemented...\n";
}
void PasteOMaticView::_StartPaste(entry_ref ref)
{
PasteOMaticPaster *paster;
char *type;
off_t size;
BNode node(&ref);
BFile file(&ref, B_READ_ONLY);
attr_info attrInfo;
BMessage message;
BString errorString;
node.GetAttrInfo("BEOS:TYPE", &attrInfo);
type = new char[attrInfo.size];
node.ReadAttr("BEOS:TYPE", B_STRING_TYPE, 0, type, attrInfo.size);
file.GetSize(&size);
paster = _FindPaster(type, (size_t)size);
if (!paster)
{
errorString << "Can't handle type (" << type << ") or size (" << size << " bytes)";
message.what = MESSAGE_PASTE_FAIL;
message.AddString("error", errorString.String());
MessageReceived(&message);
return;
}
paster->Run();
message.what = MESSAGE_PASTE_REF;
message.AddRef("ref", &ref);
paster->PostMessage(&message);
}