Skip to content
This repository was archived by the owner on Mar 2, 2025. It is now read-only.

Commit ed9dd21

Browse files
author
Humdinger
committed
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.
1 parent 95b446e commit ed9dd21

File tree

2 files changed

+47
-23
lines changed

2 files changed

+47
-23
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ RSRCS =
5454
# - if your library does not follow the standard library naming scheme,
5555
# you need to specify the path to the library and it's name.
5656
# (e.g. for mylib.a, specify "mylib.a" or "path/mylib.a")
57-
LIBS = be localestub $(STDCPPLIBS)
57+
LIBS = be localestub bnetapi $(STDCPPLIBS)
5858

5959
# Specify additional paths to directories following the standard libXXX.so
6060
# or libXXX.a naming scheme. You can specify full paths or paths relative

main.cpp

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
* Copyright 2018. All rights reserved.
33
* Distributed under the terms of the MIT license.
44
*
5-
* Authors:
5+
* Author:
66
* Humdinger, [email protected]
7-
* Werner Freytag, [email protected]
87
*
98
* Modified code from CopyNameToClipboard
9+
* _CheckNetworkConnection() borrowed from SoftwareUpdater
1010
*/
1111

1212
#include <stdio.h>
@@ -15,13 +15,51 @@
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+
2563
extern "C" void
2664
process_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

Comments
 (0)