-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use Haiku API to check if connection is up
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
Showing
2 changed files
with
47 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,11 +2,11 @@ | |
* Copyright 2018. All rights reserved. | ||
* Distributed under the terms of the MIT license. | ||
* | ||
* Authors: | ||
* Author: | ||
* Humdinger, [email protected] | ||
* Werner Freytag, [email protected] | ||
* | ||
* Modified code from CopyNameToClipboard | ||
* _CheckNetworkConnection() borrowed from SoftwareUpdater | ||
*/ | ||
|
||
#include <stdio.h> | ||
|
@@ -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*) | ||
{ | ||
|
@@ -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()); | ||
} | ||
|