Skip to content

Commit

Permalink
Use Haiku API to check if connection is up
Browse files Browse the repository at this point in the history
Borrowed code from Haiku's Softwareupdater to more elegantly
check if the network is up. Much quicker!

Use a CopyToClipboard function as it's now used more than once.
  • Loading branch information
Humdinger committed Sep 3, 2018
1 parent 95b446e commit ed9dd21
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
68 changes: 46 additions & 22 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>
Expand All @@ -15,13 +15,51 @@
#include <Catalog.h>
#include <Clipboard.h>
#include <Entry.h>
#include <NetworkInterface.h>
#include <NetworkRoster.h>
#include <Path.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;
}


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*)
{
Expand All @@ -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());
}
Expand Down

0 comments on commit ed9dd21

Please sign in to comment.