diff --git a/Makefile b/Makefile index b5bc986..bd4a0e7 100644 --- a/Makefile +++ b/Makefile @@ -54,7 +54,7 @@ RSRCS = # - if your library does not follow the standard library naming scheme, # you need to specify the path to the library and it's name. # (e.g. for mylib.a, specify "mylib.a" or "path/mylib.a") -LIBS = be localestub $(STDCPPLIBS) +LIBS = be localestub bnetapi $(STDCPPLIBS) # Specify additional paths to directories following the standard libXXX.so # or libXXX.a naming scheme. You can specify full paths or paths relative diff --git a/main.cpp b/main.cpp index 11fd530..5994261 100644 --- a/main.cpp +++ b/main.cpp @@ -2,11 +2,11 @@ * Copyright 2018. All rights reserved. * Distributed under the terms of the MIT license. * - * Authors: + * Author: * Humdinger, humdingerb@gmail.com - * Werner Freytag, freytag@gmx.de * * Modified code from CopyNameToClipboard + * _CheckNetworkConnection() borrowed from SoftwareUpdater */ #include @@ -15,6 +15,8 @@ #include #include #include +#include +#include #include #include @@ -22,6 +24,42 @@ #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; +} + + +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*) { @@ -33,30 +71,16 @@ process_refs(entry_ref directoryRef, BMessage* msg, void*) if (entry.IsDirectory()) { BString text(B_TRANSLATE( "UploadIt only works on a single file, no folders")); - 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(); - } + CopyToClipboard(text); + } else if (CheckNetworkConnection() == false) { + BString text(B_TRANSLATE( + "Online upload service not available")); + CopyToClipboard(text); } else { entry.GetPath(&path); BString command( - "stat=$(curl -m 2 -s -I http://google.com | grep HTTP/1 | awk {'print $2'}) ; " - "if [ -z \"$stat\" ] ; then " // network up in general? - "clipboard -c \"%ERROR%\" ; " - "else " - "curl -F'file=@'\"%FILENAME%\" https://0x0.st | clipboard -i ; " - "fi ; " + "curl -F'file=@'\"%FILENAME%\" https://0x0.st | clipboard -i ; " "exit"); - command.ReplaceAll("%ERROR%", - B_TRANSLATE("Online upload service not available")); command.ReplaceAll("%FILENAME%", path.Path()); system(command.String()); }