File: PluginHandler.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 (147 lines) | stat: -rw-r--r-- 5,691 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
/******************************************************************************
 *
 * 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.         *
 ***************************************************************************
 */

/**
 * Handle plugin install from remote repositories and local operations
 * to uninstall and list plugins. The plugin maintains an internal list
 * and is a singleton.
 *
 * Remote repositories are based on XML files describing metadata and
 * addresses to tarballs with compiled and installed files, basically
 * under there different directories.
 *
 *    - The plugin directory e. g., /usr/lib/opencpn/
 *    - The data directory e. g., /usr/share/data/opencpn/plugins.
 *    - The binary directory (additional program) e. g, /usr/bin.
 *
 * The compiled plugins are installed to user-writable location(s)
 *    
 *    - Linux: under ~/.local, possibly relocated by environment variables.
 *    - Windows: under &GetWinPluginBaseDir() which is configurable,
 *      defaults to %APPDATA%/Local/opencpn
 *    - Flatpak: under ~/.var/app/org.opencpn.OpenCPN
 *
 * However, plugins are loaded from multiple locations basically 
 * corresponding to new and old lacations:
 *
 *    - Windows: GetSharedDataDirPtr():GetWinPluginBaseDir()
 *    - linux: As defined  by XDG_DATA_DIRS, defaulting to
 *      ~/.local/lib/opencpn:/usr/local/lib/opencpn:/usr/lib/opencpn
 *    - flatpak:
 *        /app/lib/opencpn:/app/extensions/lib/opencpn:~/.var/app/opencpn/lib
 *        
 */

#ifndef PLUGIN_HANDLER_H__
#define PLUGIN_HANDLER_H__

#include "config.h"

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

#include <wx/cmdline.h>

#include <archive.h>

#include "catalog_parser.h"

bool isRegularFile(const char* path);





class PluginHandler {

    public:
        static PluginHandler* getInstance();

        /** Cleanup failed installation attempt using filelist for plugin. */
        static void
            cleanup(const std::string& filelist, const std::string& plugname);
        static void
            cleanupFiles(const std::string& manifestFile, const std::string& plugname);

        /** Return path to installation manifest for given plugin. */
        static std::string fileListPath(std::string name);

        /** Return path to file containing version for given plugin. */
        static std::string versionPath(std::string name);

        /** Return true if given plugin is loadable on given os/version. */
        static bool isCompatible(const PluginMetadata& metadata,
                                 const char* os = PKG_TARGET,
                                 const char* os_version = PKG_TARGET_VERSION);

        /** Check if given plugin can be installed/updated. */
        bool isPluginWritable(std::string name);

        /** Return list of all installed plugins. */
        const std::vector<PluginMetadata> getInstalled();

        /** Return list of available, not installed plugins. */
        const std::vector<PluginMetadata> getAvailable();

        /** Return path to metadata XML file. */
        std::string getMetadataPath();

        /** Set path to metadata XML file. */
        void setMetadata(std::string path) { metadataPath = path; }

        /** Download and install a new, not installed plugin. */
        bool installPlugin(PluginMetadata plugin);

        /** Install a new, downloaded but not installed plugin tarball. */
        bool installPlugin(PluginMetadata plugin, std::string path);

	/** Uninstall an installed plugin. */
        bool uninstall(const std::string plugin);

         /** Install plugin tarball from local cache. */
        bool installPluginFromCache( PluginMetadata plugin );

        std::string getLastErrorMsg() { return last_error_msg; }

        CatalogData *GetCatalogData(){ return &catalogData; }
        
    protected:
	/** Initiats the handler and set up LD_LIBRARY_PATH. */
        PluginHandler() {}

    private:
        std::string metadataPath;
        std::vector<PluginMetadata> installed;
        CatalogData catalogData;
        std::string last_error_msg;
        bool explodeTarball(struct archive* src,
                            struct archive* dest,
                            std::string& filelist);
        bool extractTarball(const std::string path, std::string& filelist);
        bool archive_check(int r, const char* msg, struct archive* a);
};


#endif // PLUGIN_HANDLER_H__