-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
160 lines (134 loc) · 3.93 KB
/
main.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
/*
* Copyright 2018-2023. All rights reserved.
* Distributed under the terms of the MIT license.
*
* Author:
* Humdinger, [email protected]
*
* Modified code from CopyNameToClipboard
* _CheckNetworkConnection() borrowed from SoftwareUpdater
*/
#include <stdio.h>
#include <stdlib.h>
#include <Alert.h>
#include <Application.h>
#include <Bitmap.h>
#include <Catalog.h>
#include <Clipboard.h>
#include <Entry.h>
#include <IconUtils.h>
#include <NetworkInterface.h>
#include <NetworkRoster.h>
#include <Notification.h>
#include <Path.h>
#include <Rect.h>
#include <Roster.h>
#include <String.h>
#undef B_TRANSLATION_CONTEXT
#define B_TRANSLATION_CONTEXT "Add-On"
bool
CheckNetworkConnection()
{
BNetworkRoster& roster = BNetworkRoster::Default();
BNetworkInterface interface;
uint32 cookie = 0;
while (roster.GetNextInterface(&cookie, interface) == B_OK) {
uint32 flags = interface.Flags();
if ((flags & IFF_LOOPBACK) == 0 && (flags & (IFF_UP | IFF_LINK)) == (IFF_UP | IFF_LINK))
return true;
}
// No network connection detected, cannot continue
return false;
}
BString
GetStdoutFromCommand(BString cmd)
{
BString data;
FILE* stream;
const int max_buffer = 256;
char buffer[max_buffer];
stream = popen(cmd.String(), "r");
if (stream) {
while (!feof(stream))
if (fgets(buffer, max_buffer, stream) != NULL)
data.Append(buffer);
pclose(stream);
}
return data;
}
void
CopyToClipboard(BString text)
{
ssize_t textLen = text.Length();
BMessage* message = (BMessage*) NULL;
if (be_clipboard->Lock()) {
be_clipboard->Clear();
if ((message = be_clipboard->Data())) {
message->AddData("text/plain", B_MIME_TYPE, text.String(), textLen);
be_clipboard->Commit();
}
be_clipboard->Unlock();
}
}
extern "C" void
process_refs(entry_ref directoryRef, BMessage* msg, void*)
{
BPath path;
entry_ref file_ref;
// Add time for a reasonably unique MessageID
bigtime_t time(real_time_clock());
BString msgID("UploadIt-");
msgID << time;
BNotification notification(B_INFORMATION_NOTIFICATION);
notification.SetMessageID(msgID);
notification.SetGroup("UploadIt");
BBitmap* icon = new BBitmap(BRect(0, 0, B_LARGE_ICON - 1, B_LARGE_ICON - 1), B_RGBA32);
BMimeType ourself("application/x-vnd.humdinger-UploadIt");
if (ourself.GetIcon(icon, B_LARGE_ICON) == B_OK)
notification.SetIcon(icon);
if (msg->FindRef("refs", &file_ref) == B_NO_ERROR) {
BString title;
BString content;
if (CheckNetworkConnection() == false) {
title = B_TRANSLATE("Upload failed");
content = B_TRANSLATE("Online service not available");
} else {
BEntry entry(&file_ref);
entry.GetPath(&path);
notification.SetTitle(B_TRANSLATE("Uploading " B_UTF8_ELLIPSIS));
notification.SetContent(path.Leaf());
notification.Send(600000000);
BString command("curl -F 'file=@'\"%FILEPATH%\" https://0x0.st");
if (entry.IsDirectory()) {
BPath parent;
if (path.GetParent(&parent) == B_OK) {
command.Prepend("cd \"%PARENTPATH%\" && zip -qry - \"%FOLDERNAME%\" | ");
command.ReplaceFirst("%PARENTPATH%", parent.Path());
command.ReplaceFirst("%FOLDERNAME%", path.Leaf());
command.ReplaceLast("%FILEPATH%", "-");
}
} else
command.ReplaceFirst("%FILEPATH%", path.Path());
content = GetStdoutFromCommand(command.String());
content.ReplaceLast("\n", "");
title = B_TRANSLATE("Upload failed");
if (content.FindFirst("413 Request Entity Too Large") != B_ERROR) {
content = B_TRANSLATE("'%FILENAME%' is too large");
content.ReplaceFirst("%FILENAME%", path.Leaf());
} else if (content.FindFirst("https://0x0.st/") == 0) {
title = (B_TRANSLATE_COMMENT("Finished. URL in clipboard:",
"As short as possible, not much space in a Notification."));
}
}
notification.SetTitle(title);
notification.SetContent(content);
notification.Send();
CopyToClipboard(content);
}
}
int
main(int, char**)
{
fprintf(stderr, B_TRANSLATE("UploadIt can only be used as a Tracker add-on.\n"));
return -1;
}