File: tqslhelp.cpp

package info (click to toggle)
trustedqsl 2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 29,116 kB
  • ctags: 3,577
  • sloc: cpp: 24,858; xml: 5,344; ansic: 626; sh: 88; python: 18; makefile: 11
file content (112 lines) | stat: -rw-r--r-- 3,024 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/***************************************************************************
                          tqslhelp.cpp  -  description
                             -------------------
          copyright (C) 2013-2016 by ARRL and the TrustedQSL Developers
 ***************************************************************************/

//Derived from wxWidgets fs_inet.cpp
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif

#if !wxUSE_SOCKETS
    #undef wxUSE_FS_INET
    #define wxUSE_FS_INET 0
#endif

#if wxUSE_FILESYSTEM && wxUSE_FS_INET

#ifndef WXPRECOMP
    #include "wx/module.h"
#endif

#include "wx/wfstream.h"
#include "wx/url.h"
#include "wx/filesys.h"
#include "wx/utils.h"
#include "wx/mstream.h"
#include "tqslhelp.h"
#include "tqsltrace.h"

// ----------------------------------------------------------------------------
// tqslInternetFSHandler
// ----------------------------------------------------------------------------

static wxString StripProtocolAnchor(const wxString& location) {
	tqslTrace("StripProtocolAnchor", "location=%s", S(location));
	wxString myloc(location.BeforeLast(wxT('#')));
	if (myloc.empty())
		myloc = location.AfterFirst(wxT(':'));
	else
		myloc = myloc.AfterFirst(wxT(':'));

	// fix malformed url
	if (!myloc.Left(2).IsSameAs(wxT("//"))) {
		if (myloc.GetChar(0) != wxT('/'))
			myloc = wxT("//") + myloc;
		else
			myloc = wxT("/") + myloc;
	}
	if (myloc.Mid(2).Find(wxT('/')) == wxNOT_FOUND) myloc << wxT('/');

	return myloc;
}

//
// Simple filesystem "handler" that notices full URLs and
// opens them in the user's default browser.
//
// Always returns an error indication so that the help widget doesn't
// think it's actually been handled.
//
bool tqslInternetFSHandler::CanOpen(const wxString& location) {
	tqslTrace("tqslInternetFSHandler::CanOpen", "location=%s", S(location));
	static wxString lastLocation;
#if wxUSE_URL
	wxString p = GetProtocol(location);
	if ((p == wxT("http")) || (p == wxT("ftp"))) {
		// Keep track of the last location as we get
		// repeated attempts to open - only open
		// the page once.
		if (location != lastLocation) {
			wxString right = GetProtocol(location) + wxT(":") +
			StripProtocolAnchor(location);
			wxLaunchDefaultBrowser(right);
		}
		lastLocation = location;
	}
#endif
	return false;
}

wxFSFile* tqslInternetFSHandler::OpenFile(wxFileSystem& WXUNUSED(fs), const wxString& location) {
	return NULL; // We never actually return anything
}

class tqslFileSystemInternetModule : public wxModule {
	DECLARE_DYNAMIC_CLASS(tqslFileSystemInternetModule)

 public:
	tqslFileSystemInternetModule()
	    : wxModule(), m_handler(NULL) {
	}

	virtual bool OnInit() {
	    m_handler = new tqslInternetFSHandler;
	    wxFileSystem::AddHandler(m_handler);
	    return true;
	}

	virtual void OnExit() {
	    delete wxFileSystem::RemoveHandler(m_handler);
	}

 private:
	wxFileSystemHandler* m_handler;
};

IMPLEMENT_DYNAMIC_CLASS(tqslFileSystemInternetModule, wxModule)

#endif // wxUSE_FILESYSTEM && wxUSE_FS_INET