22 * Copyright 2018. All rights reserved.
33 * Distributed under the terms of the MIT license.
44 *
5- * Authors :
5+ * Author :
66 * Humdinger, humdingerb@gmail.com
7- * Werner Freytag, freytag@gmx.de
87 *
98 * Modified code from CopyNameToClipboard
9+ * _CheckNetworkConnection() borrowed from SoftwareUpdater
1010 */
1111
1212#include < stdio.h>
1515#include < Catalog.h>
1616#include < Clipboard.h>
1717#include < Entry.h>
18+ #include < NetworkInterface.h>
19+ #include < NetworkRoster.h>
1820#include < Path.h>
1921#include < String.h>
2022
2123#undef B_TRANSLATION_CONTEXT
2224#define B_TRANSLATION_CONTEXT " Add-On"
2325
2426
27+ bool
28+ CheckNetworkConnection ()
29+ {
30+ BNetworkRoster& roster = BNetworkRoster::Default ();
31+ BNetworkInterface interface;
32+ uint32 cookie = 0 ;
33+ while (roster.GetNextInterface (&cookie, interface) == B_OK) {
34+ uint32 flags = interface.Flags ();
35+ if ((flags & IFF_LOOPBACK) == 0
36+ && (flags & (IFF_UP | IFF_LINK)) == (IFF_UP | IFF_LINK)) {
37+ return true ;
38+ }
39+ }
40+ // No network connection detected, cannot continue
41+ return false ;
42+ }
43+
44+
45+ void
46+ CopyToClipboard (BString text)
47+ {
48+ ssize_t textLen = text.Length ();
49+ BMessage* message = (BMessage *)NULL ;
50+
51+ if (be_clipboard->Lock ()) {
52+ be_clipboard->Clear ();
53+ if ((message = be_clipboard->Data ())) {
54+ message->AddData (" text/plain" , B_MIME_TYPE, text.String (),
55+ textLen);
56+ be_clipboard->Commit ();
57+ }
58+ be_clipboard->Unlock ();
59+ }
60+ }
61+
62+
2563extern " C" void
2664process_refs (entry_ref directoryRef, BMessage* msg, void *)
2765{
@@ -33,30 +71,16 @@ process_refs(entry_ref directoryRef, BMessage* msg, void*)
3371 if (entry.IsDirectory ()) {
3472 BString text (B_TRANSLATE (
3573 " UploadIt only works on a single file, no folders" ));
36- ssize_t textLen = text.Length ();
37- BMessage* message = (BMessage *)NULL ;
38-
39- if (be_clipboard->Lock ()) {
40- be_clipboard->Clear ();
41- if ((message = be_clipboard->Data ())) {
42- message->AddData (" text/plain" , B_MIME_TYPE, text.String (),
43- textLen);
44- be_clipboard->Commit ();
45- }
46- be_clipboard->Unlock ();
47- }
74+ CopyToClipboard (text);
75+ } else if (CheckNetworkConnection () == false ) {
76+ BString text (B_TRANSLATE (
77+ " Online upload service not available" ));
78+ CopyToClipboard (text);
4879 } else {
4980 entry.GetPath (&path);
5081 BString command (
51- " stat=$(curl -m 2 -s -I http://google.com | grep HTTP/1 | awk {'print $2'}) ; "
52- " if [ -z \" $stat\" ] ; then " // network up in general?
53- " clipboard -c \" %ERROR%\" ; "
54- " else "
55- " curl -F'file=@'\" %FILENAME%\" https://0x0.st | clipboard -i ; "
56- " fi ; "
82+ " curl -F'file=@'\" %FILENAME%\" https://0x0.st | clipboard -i ; "
5783 " exit" );
58- command.ReplaceAll (" %ERROR%" ,
59- B_TRANSLATE (" Online upload service not available" ));
6084 command.ReplaceAll (" %FILENAME%" , path.Path ());
6185 system (command.String ());
6286 }
0 commit comments