1 /******************************************************************************
2  * $Id: chartdldr_pi.h,v 1.0 2011/02/26 01:54:37 nohal Exp $
3  *
4  * Project:  OpenCPN
5  * Purpose:  Chart Downloader Plugin
6  * Author:   Pavel Kalian
7  *
8  ***************************************************************************
9  *   Copyright (C) 2011 by Pavel Kalian   *
10  *   $EMAIL$   *
11  *                                                                         *
12  *   This program is free software; you can redistribute it and/or modify  *
13  *   it under the terms of the GNU General Public License as published by  *
14  *   the Free Software Foundation; either version 2 of the License, or     *
15  *   (at your option) any later version.                                   *
16  *                                                                         *
17  *   This program is distributed in the hope that it will be useful,       *
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
20  *   GNU General Public License for more details.                          *
21  *                                                                         *
22  *   You should have received a copy of the GNU General Public License     *
23  *   along with this program; if not, write to the                         *
24  *   Free Software Foundation, Inc.,                                       *
25  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
26  ***************************************************************************
27  */
28 
29 #ifndef _CHARTDLDRPI_H_
30 #define _CHARTDLDRPI_H_
31 
32 #include "wx/wxprec.h"
33 
34 #ifndef  WX_PRECOMP
35   #include "wx/wx.h"
36 #endif //precompiled headers
37 
38 #include <wx/fileconf.h>
39 #include <wx/tokenzr.h>
40 #include <wx/event.h>
41 
42 #include <wx/imaglist.h>
43 
44 #include <map>
45 
46 #include "version.h"
47 
48 #define     MY_API_VERSION_MAJOR    1
49 #define     MY_API_VERSION_MINOR    13
50 
51 #define USERDATA "{USERDATA}"
52 
53 #include "ocpn_plugin.h"
54 
55 #include "chartdldrgui.h"
56 #include "chartcatalog.h"
57 
58 #define UPDATE_DATA_FILENAME "chartdldr_pi.dat"
59 
60 // forward declarations
61 class ChartSource;
62 class ChartDldrPanelImpl;
63 class ChartDldrGuiAddSourceDlg;
64 class ChartDldrPrefsDlgImpl;
65 
66 WX_DEFINE_ARRAY_PTR(ChartSource *, wxArrayOfChartSources);
67 WX_DECLARE_OBJARRAY(wxDateTime, wxArrayOfDateTime);
68 
69 #define NEW_LIST
70 
71 //----------------------------------------------------------------------------------------------------------
72 //    The PlugIn Class Definition
73 //----------------------------------------------------------------------------------------------------------
74 
75 class chartdldr_pi : public opencpn_plugin_113
76 {
77 public:
78     chartdldr_pi( void *ppimgr );
79 
80 //    The required PlugIn Methods
81     int             Init( void );
82     bool            DeInit( void );
83 
84     int             GetAPIVersionMajor();
85     int             GetAPIVersionMinor();
86     int             GetPlugInVersionMajor();
87     int             GetPlugInVersionMinor();
88     wxBitmap       *GetPlugInBitmap();
89     wxString        GetCommonName();
90     wxString        GetShortDescription();
91     wxString        GetLongDescription();
92 
93     void            OnSetupOptions( void );
94     void            OnCloseToolboxPanel( int page_sel, int ok_apply_cancel );
95 
96 //    The required override PlugIn Methods
97     void            ShowPreferencesDialog( wxWindow* parent );
98 
99 //    Other public methods
100     bool            SaveConfig(void);
101     bool            ProcessFile(const wxString& aFile, const wxString& aTargetDir, bool aStripPath = true, wxDateTime aMTime = wxDateTime::Now());
102     bool            ExtractZipFiles(const wxString& aZipFile, const wxString& aTargetDir, bool aStripPath = true, wxDateTime aMTime = wxDateTime::Now(), bool aRemoveZip = false);
103 #ifdef DLDR_USE_LIBARCHIVE
104     bool            ExtractLibArchiveFiles(const wxString& aArchiveFile, const wxString& aTargetDir, bool aStripPath = true, wxDateTime aMTime = wxDateTime::Now(), bool aRemoveArchive = false);
105 #endif
106 #if defined(CHARTDLDR_RAR_UNARR) || !defined(DLDR_USE_LIBARCHIVE)
107     bool            ExtractUnarrFiles(const wxString& aRarFile, const wxString& aTargetDir, bool aStripPath = true, wxDateTime aMTime = wxDateTime::Now(), bool aRemoveRar = false);
108 #endif
109 
110     void            UpdatePrefs(ChartDldrPrefsDlgImpl *dialog);
111 
112 //    Public properties
113     wxArrayOfChartSources *m_pChartSources;
114     wxWindow       *m_parent_window;
115     ChartCatalog   *m_pChartCatalog;
116     ChartSource    *m_pChartSource;
SetSourceId(int id)117     void            SetSourceId(int id) { m_selected_source = id; }
GetSourceId()118     int             GetSourceId() { return m_selected_source; }
GetBaseChartDir()119     wxString        GetBaseChartDir() { return m_base_chart_dir; }
120     bool            m_preselect_new;
121     bool            m_preselect_updated;
122     bool            m_allow_bulk_update;
123 
124 private:
125     wxFileConfig   *m_pconfig;
126     wxScrolledWindow *m_pOptionsPage;
127     bool            LoadConfig(void);
128 
129     int             m_leftclick_tool_id;
130 
131     wxString        m_schartdldr_sources;
132     int             m_selected_source;
133 
134     ChartDldrPanelImpl *m_dldrpanel;
135     wxString        m_base_chart_dir;
136 };
137 
138 class ChartSource : public wxTreeItemData
139 {
140 public:
141     ChartSource( wxString name, wxString url, wxString localdir );
142     ~ChartSource();
143 
GetName()144     wxString        GetName() { return m_name; }
GetUrl()145     wxString        GetUrl() { return m_url; }
GetDir()146     wxString        GetDir() { return m_dir; }
SetDir(wxString dir)147     void            SetDir( wxString dir ) { m_dir = dir; }
SetName(wxString name)148     void            SetName(    wxString name ) { m_name = name; }
SetUrl(wxString url)149     void            SetUrl( wxString url ) { m_url = url; }
150     bool            ExistsLocaly(   wxString chart_number, wxString filename );
151     bool            IsNewerThanLocal( wxString chart_number, wxString filename, wxDateTime validDate );
UpdateLocalFiles()152     void            UpdateLocalFiles() { GetLocalFiles(); }
153 
154     bool            UpdateDataExists();
155     void            LoadUpdateData();
156     void            SaveUpdateData();
157     void            ChartUpdated( wxString chart_number, time_t timestamp );
158 private:
159     wxArrayString   m_localfiles;
160     wxArrayOfDateTime m_localdt;
161     void            GetLocalFiles();
162     wxString        m_name;
163     wxString        m_url;
164     wxString        m_dir;
165     std::map<std::string, time_t> m_update_data;
166 };
167 
168 /** Implementing ChartDldrPanel */
169 class ChartDldrPanelImpl : public ChartDldrPanel
170 {
171 friend class chartdldr_pi;
172 private:
173     bool            DownloadChart( wxString url, wxString file, wxString title );
174     bool            downloadInProgress;
175     int             to_download;
176 
177     int             updatingAll;
178     bool            cancelled;
179     bool            DownloadIsCancel;
180     chartdldr_pi   *pPlugIn;
181     bool            m_populated;
182 
183     void            OnPopupClick( wxCommandEvent &evt );
184     int             GetSelectedCatalog();
185     void            AppendCatalog(ChartSource *cs);
186     void            DoEditSource();
187 
188     bool            m_bTransferComplete;
189     bool            m_bTransferSuccess;
190     wxString        m_totalsize;
191     wxString        m_transferredsize;
192     int		    m_failed_downloads;
193     int             m_downloading;
194 
195     void            DisableForDownload( bool enabled );
196     bool            m_bconnected;
197 
198 protected:
199     // Handlers for ChartDldrPanel events.
200     void            SetSource( int id );
201 	void            SelectSource( wxListEvent& event );
202 	void            AddSource( wxCommandEvent& event );
203 	void            DeleteSource( wxCommandEvent& event );
204 	void            EditSource( wxCommandEvent& event );
205 	void            UpdateChartList( wxCommandEvent& event );
206 	void            OnDownloadCharts( wxCommandEvent& event );
207 	void            DownloadCharts( );
DoHelp(wxCommandEvent & event)208 	void            DoHelp( wxCommandEvent& event )
209       {
210           #ifdef __WXMSW__
211           wxLaunchDefaultBrowser( _T("file:///") + *GetpSharedDataLocation() + _T("plugins/chartdldr_pi/data/doc/index.html") );
212           #else
213           wxLaunchDefaultBrowser( _T("file://") + *GetpSharedDataLocation() + _T("plugins/chartdldr_pi/data/doc/index.html") );
214           #endif
215       }
216     void            UpdateAllCharts( wxCommandEvent& event );
217 	void            OnShowLocalDir( wxCommandEvent& event );
218     void            OnPaint( wxPaintEvent& event );
219     void            OnLeftDClick( wxMouseEvent& event );
220 
221     void            CleanForm();
222     void            FillFromFile( wxString url, wxString dir, bool selnew = false, bool selupd = false );
223 
224     void            OnContextMenu( wxMouseEvent& event );
225     void            SetBulkUpdate( bool bulk_update );
226 
227     int             GetChartCount();
228     int             GetCheckedChartCount();
229     bool            isChartChecked( int i );
230     void            CheckAllCharts( bool value );
231     void            InvertCheckAllCharts( );
232 #ifdef NEW_LIST
233     void            CheckNewCharts( bool value );
234     void            CheckUpdatedCharts(bool value);
235 #endif	/* NEW_LIST */
236 
237 public:
238     //ChartDldrPanelImpl() { m_bconnected = false; DownloadIsCancel = false; }
239     ~ChartDldrPanelImpl();
240     ChartDldrPanelImpl( chartdldr_pi* plugin = NULL, wxWindow* parent = NULL, wxWindowID id = wxID_ANY, const wxPoint& pos = wxDefaultPosition, const wxSize& size = wxDefaultSize, long style = wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER );
241     void            SelectCatalog( int item );
242     void            onDLEvent(OCPN_downloadEvent &ev);
CancelDownload()243     void            CancelDownload() { Disconnect(wxEVT_DOWNLOAD_EVENT, (wxObjectEventFunction)(wxEventFunction)&ChartDldrPanelImpl::onDLEvent); cancelled = true; m_bconnected = false;}
244 
245 private:
246     DECLARE_DYNAMIC_CLASS( ChartDldrPanelImpl )
247     DECLARE_EVENT_TABLE()
248 };
249 
250 class ChartDldrGuiAddSourceDlg : public AddSourceDlg
251 {
252 protected:
253     void            OnChangeType( wxCommandEvent& event );
254 	void            OnSourceSelected( wxTreeEvent& event );
255 	void            OnOkClick( wxCommandEvent& event );
256     void            OnCancelClick( wxCommandEvent& event );
257 
258     bool            LoadSources();
259     bool            LoadSections( const wxTreeItemId &root, pugi::xml_node &node );
260     bool            LoadSection( const wxTreeItemId &root, pugi::xml_node &node );
261     bool            LoadCatalogs( const wxTreeItemId &root, pugi::xml_node &node );
262     bool            LoadCatalog( const wxTreeItemId &root, pugi::xml_node &node );
263 
264 public:
265     ChartDldrGuiAddSourceDlg( wxWindow* parent );
266 	~ChartDldrGuiAddSourceDlg();
SetBasePath(const wxString path)267 	void            SetBasePath( const wxString path ) { m_base_path = path; }
268     void            SetSourceEdit( ChartSource* cs );
269 
270 private:
271     bool            ValidateUrl(const wxString Url, bool catalog_xml = true);
272     wxString        FixPath(wxString path);
273     wxString        m_base_path;
274     wxString        m_last_path;
275     wxImageList    *p_iconList;
276 #ifdef __OCPN__ANDROID__
277     wxImageList    *p_buttonIconList;
278 #endif /* __OCPN__ANDROID__ */
279 };
280 
281 class ChartDldrPrefsDlgImpl : public ChartDldrPrefsDlg
282 {
283 protected:
284     void            OnOkClick( wxCommandEvent& event );
285 
286 public:
287     ChartDldrPrefsDlgImpl( wxWindow* parent );
288 	~ChartDldrPrefsDlgImpl();
GetPath()289 	wxString        GetPath() { return m_tcDefaultDir->GetValue(); }
290 	void            SetPath( const wxString path );
291 	void            GetPreferences( bool &preselect_new, bool &preselect_updated, bool &bulk_update );
292 	void            SetPreferences( bool preselect_new, bool preselect_updated, bool bulk_update );
293 };
294 
295 #endif
296