File: catalog_handler.h

package info (click to toggle)
opencpn 5.2.4%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 101,540 kB
  • sloc: ansic: 414,598; cpp: 253,008; xml: 83,748; sh: 409; python: 353; makefile: 110; javascript: 87; perl: 83
file content (148 lines) | stat: -rw-r--r-- 5,379 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/******************************************************************************
 *
 * Project:  OpenCPN
 *
 ***************************************************************************
 *   Copyright (C) 2019 Alec Leamas                                        *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,  USA.         *
 ***************************************************************************
 */

/**
 * Plugin catalog management: Check for available versions and branches,
 * download as required.
 */

#ifndef CATALOG_HANDLER_H__
#define CATALOG_HANDLER_H__

#include <string>
#include <memory>
#include <ostream>
#include <vector>

#include "catalog_parser.h"


/**
 * A local proxy for the catalog server. The server has a number
 * of branches, some of which containing a plugin catalog.
 *
 * Backend code for channel management (which catalog to get) and
 * the important download function. 
 *
 * Also: CatalogData handling, basically version and date for 
 * various ocpn-plugins.xml.
 */
class CatalogHandler {

    public:
        
        enum class ServerStatus {
            UNKNOWN, OK, OK_MSG, CURL_ERROR, JSON_ERROR, XML_ERROR, OS_ERROR
        };

        static CatalogHandler* getInstance();

        /** Download channel json data, possibly return error code. */
        ServerStatus LoadChannels(std::ostream* json);

        /** Parse and store json channel data, possibly return error code. */
        ServerStatus LoadChannels(const std::string& json);

        /** Get the downloaded list of channels, empty on errors. */
        std::vector<std::string> GetChannels();

        /** Set the active channel used when downloading catalog. */
        bool SetActiveChannel(const char* channel);

        /** Get the branch (a. k. a. channel) used to download. */
        std::string GetActiveChannel();

        /** Set a custom url, overrides also channel settings. */
        void SetCustomUrl(const char* url);

        /** Set a custom url, overrides also channel settings. */
        std::string  GetCustomUrl();

        /** Get the default URL, with actual channel included */
        std::string GetDefaultUrl();

        /** Download the latest catalog to given stream. */
        ServerStatus DownloadCatalog(std::ostream* stream);

        /** Download the latest catalog to local path. */
        ServerStatus DownloadCatalog(std::string& path);

        /** Download the specified catalog to given stream. */
        ServerStatus DownloadCatalog(std::ostream* stream, std::string url);

        /** Download the specified catalog to local path. */
        ServerStatus DownloadCatalog(std::string& filePath, std::string url);

        /** Parse XML contents, save as latest data if latest is true. */
        ServerStatus ParseCatalog(const std::string xml, bool latest = false);

        /** Data for default version, installed with main opencpn. */
        CatalogData DefaultCatalogData();

        /** Data for user catalog which overrides the default one. */
        CatalogData UserCatalogData();

        /** Data for latest parsed data marked as latest. */
        CatalogData LatestCatalogData();

        /** Invalidate *CatalogData caches */
        void ClearCatalogData();

        /** Last error message, free format. */
        std::string LastErrorMsg();

        ServerStatus DoParseCatalog(const std::string xml, catalog_ctx* ctx);

    protected:
	/** Initiate the handler. */
        CatalogHandler();

        void LoadCatalogData(const std::string& path, CatalogData& data);

        const char* const GET_BRANCHES_PATH =
            "/repos/OpenCPN/plugins/branches";
        const char* const GITHUB_API =
            "https://api.github.com";

        const char* const REPO_URL = 
            "https://raw.githubusercontent.com";
        const char* const REPO_PATH = 
            "/OpenCPN/plugins/@branch@/ocpn-plugins.xml";

        const char* const DEFAULT_CHANNEL = "master";

    private:
        std::vector<std::string> channels;
        ServerStatus status;
        std::ostream* stream;
        std::string error_msg;
        CatalogData latest_data;
        CatalogData default_data;
        CatalogData user_data;
};

typedef CatalogHandler::ServerStatus catalog_status;


#endif // CATALOG_HANDLER_H__