summaryrefslogtreecommitdiff
path: root/pcbnew/dialogs/wizard_add_fplib.h
blob: 94ba00b085dc48cacf6e99400cdaa1a353bf45b6 (plain)
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2015 CERN
 * @author Maciej Suminski <maciej.suminski@cern.ch>
 * Copyright (C) 2014-2015 Jean-Pierre Charras, jp.charras at wanadoo.fr
 * Copyright (C) 1992-2015 KiCad Developers, see AUTHORS.txt for contributors.
 *
 * 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, you may find one here:
 * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
 * or you may search the http://www.gnu.org website for the version 2 license,
 * or you may write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 */

#include <wizard_add_fplib_base.h>
#include <io_mgr.h>
#include <boost/optional.hpp>

class KIWAY_PLAYER;

class WIZARD_FPLIB_TABLE : public WIZARD_FPLIB_TABLE_BASE
{
public:
    WIZARD_FPLIB_TABLE( wxWindow* aParent );
    ~WIZARD_FPLIB_TABLE();

    ///> Source of the libraries (local files or Github)
    enum LIB_SOURCE { LOCAL, GITHUB };

    ///> Scope (global/project)
    enum LIB_SCOPE { GLOBAL = 1, PROJECT = 2 };

    /**
     * Function GetFirstPage
     * Returns the welcoming page for the wizard.
     */
    inline wxWizardPage* GetFirstPage() const
    {
        return m_welcomeDlg;
    }

    /**
     * Function GetGithubURL
     * Returns the current Github repository URL set in the wizard.
     */
    inline wxString GetGithubURL() const
    {
        return m_textCtrlGithubURL->GetValue();
    }

    /**
     * Function SetGithubURL
     * Sets the current Github repository URL used by the wizard.
     * @param aUrl is the new URL to be applied.
     */
    inline void SetGithubURL( const wxString& aUrl )
    {
        m_textCtrlGithubURL->SetValue( aUrl );
    }

    /**
     * Function GetLibSource
     * Returns the source of libraries (local / Github).
     */
    LIB_SOURCE GetLibSource() const;

    /**
     * Function GetLibScope
     * Returns the scope for the added libraries (global / project specific).
     */
    LIB_SCOPE GetLibScope() const;

    // Wizard event handlers
    void OnSourceCheck( wxCommandEvent& aEvent );
    void OnSelectFiles( wxCommandEvent& aEvent );
    void OnCheckGithubList( wxCommandEvent& aEvent );
    void OnPageChanged( wxWizardEvent& aEvent );
    void OnSelectAllGH( wxCommandEvent& aEvent );
    void OnUnselectAllGH( wxCommandEvent& aEvent );
    void OnChangeSearch( wxCommandEvent& aEvent );
    void OnWizardFinished( wxWizardEvent& aEvent );
    void OnBrowseButtonClick( wxCommandEvent& aEvent );
    void OnCheckSaveCopy( wxCommandEvent& aEvent );

    class LIBRARY
    {
    public:
        LIBRARY( const wxString& aPath, const wxString& aDescription = wxEmptyString );
        ~LIBRARY()
        {
        }

        ///> Possible states of validation.
        enum STATUS { OK, INVALID, NOT_CHECKED };

        /**
         * Function Test
         * Uses the associated plugin to validate the library contents.
         * @return True if the library and the matched plugin type are valid.
         */
        bool Test();

        /**
         * Function GetPluginType
         * Returns the plugin type, autodetected basing on the path.
         * @return Returns empty boost::optional if the type could not be autodetected.
         */
        inline boost::optional<IO_MGR::PCB_FILE_T> GetPluginType() const
        {
            return m_plugin;
        }

        /**
         * Function GetPluginName
         * Returns the plugin name, as used in the FPLIB table editor.
         */
        wxString GetPluginName() const;

        /**
         * Function GetAbsolutePath
         * Returns the absolute path for the library.
         */
        inline const wxString& GetAbsolutePath() const
        {
            return m_path;
        }

        /**
         * Function GetRelativePath
         * Returns the relative path, based on the input path with the base part replaced.
         * @param aBase is the base for the relative path.
         * @param aSubstitution is the string to be replace the base path.
         * @return Adjusted path if possible, or the absolute path when it is not possible.
         */
        wxString GetRelativePath( const wxString& aBase, const wxString& aSubstitution = wxEmptyString ) const;

        /**
         * Function GetAutoPath
         * Returns path that is either absolute or related to KISYSMOD/KIPRJMOD if the files
         * are stored within one of the mentioned paths.
         * @param aScoep is the scope for the library. It determines the environmental variables
         * that are used to check the path (GLOBAL scope checks only KISYSMOD, while PROJECT
         * scope checks both KISYSMOD & KIPRJMOD).
         */
        wxString GetAutoPath( LIB_SCOPE aScope ) const;

        /**
         * Function GetDescription
         * Returns the description for the library. If it is not specified in the constructor,
         * it is just the filename.
         */
        wxString GetDescription() const;

        /**
         * Function GetStatus
         * Returns the validity status for the library. It requires running Test() before, to get
         * a result different than NOT_CHECKED.
         */
        STATUS GetStatus() const
        {
            return m_status;
        }

    protected:
        inline void setPath( const wxString& aPath )
        {
            m_path = aPath;
        }

        inline void setPluginType( IO_MGR::PCB_FILE_T aType )
        {
            m_plugin = aType;
        }

        /**
         * Function replaceEnv
         * replaces path with environmental variable if applicable.
         * @param aEnvVar is the environmental variable that should be substituted.
         * @param aFilePath determines if the path is a file path (contrary to e.g. http address).
         */
        wxString replaceEnv( const wxString& aEnvVar, bool aFilePath = true ) const;

        wxString m_path;
        wxString m_description;
        boost::optional<IO_MGR::PCB_FILE_T> m_plugin;
        STATUS m_status;

        friend class WIZARD_FPLIB_TABLE;
    };

    /**
     * Function GetLibraries
     * Returns libraries selected by the user.
     */
    const std::vector<LIBRARY>& GetLibraries() const
    {
        return m_libraries;
    }


protected:
    // Initialization of wizard pages
    void setupDialogOrder();
    void setupGithubList();
    void setupFileSelect();
    void setupReview();

    ///> Checks the selection in file picker
    bool checkFiles() const;

    ///> Sets the target directory for libraries downloaded from Github
    void setDownloadDir( const wxString& aDir )
    {
        m_downloadDir->SetLabel( aDir );
    }

    ///> Gets the current target for downloaded libraries
    inline wxString getDownloadDir()
    {
        return m_downloadDir->GetLabel();
    }

    ///> Downloads the list of Github libraries
    void getLibsListGithub( wxArrayString& aList );

    ///> Saves a list of Github libraries locally.
    bool downloadGithubLibsFromList( wxArrayString& aUrlList, wxString* aErrorMessage );

    ///> Does the user want a local copy of Github libraries?
    inline bool wantLocalCopy() const { return m_downloadGithub->GetValue(); }

    ///> Enables Github widgets depending on the selected options.
    void updateGithubControls();

    ///> Updates m_libraries basing on dialogs contents
    void updateLibraries();

    ///> Enables/disable 'Next' button
    inline void enableNext( bool aEnable )
    {
        wxWindow* nextBtn = FindWindowById( wxID_FORWARD );

        if( nextBtn )
            nextBtn->Enable( aEnable );
    }

    ///> Cache for the downloaded Github library list
    wxArrayString m_githubLibs;

    ///> Libraries selected in the wizard
    std::vector<LIBRARY> m_libraries;

    // Aliases for wizard pages to make code more readable
    wxWizardPageSimple* const m_welcomeDlg;
    wxWizardPageSimple* const m_fileSelectDlg;
    wxWizardPageSimple* const m_githubListDlg;
    wxWizardPageSimple* const m_reviewDlg;
    wxWizardPageSimple* const m_targetDlg;

    // Since the same event is used for changing the selection/filter type, we need a way to
    // determine what's the real cause of the event. Therefore here we store the number of the
    // selected file type filter.
    int m_selectedFilter;

    // path to the most recently used download directory from Github.
    wxString m_lastGithubDownloadDirectory;
};