summaryrefslogtreecommitdiff
path: root/common/gestfich.cpp
blob: 3a095768741d625d6dc97cb00c7c2839ebb893ff (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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
/*
 * This program source code file is part of KiCad, a free EDA CAD application.
 *
 * Copyright (C) 2004 Jean-Pierre Charras, jaen-pierre.charras@gipsa-lab.inpg.com
 * Copyright (C) 2008-2011 Wayne Stambaugh <stambaughw@verizon.net>
 * Copyright (C) 1992-2011 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
 */

/**
 * @file gestfich.cpp
 * @brief Functions for file management
 */

#include <wx/mimetype.h>
#include <wx/filename.h>
#include <wx/dir.h>

// For compilers that support precompilation, includes "wx.h".
#include <fctsys.h>
#include <pgm_base.h>
#include <confirm.h>
#include <common.h>
#include <macros.h>

#include <gestfich.h>

void AddDelimiterString( wxString& string )
{
    if( !string.StartsWith( wxT( "\"" ) ) )
    {
        string.Prepend ( wxT( "\"" ) );
        string.Append ( wxT( "\"" ) );
    }
}


bool EDA_PATH_SELECTOR( const wxString& aTitle,
                        wxString&       aPath,
                        int             aFlags,
                        wxWindow*       aParent,
                        const wxPoint&  aPosition )
{
    int          ii;
    bool         selected = false;

    wxDirDialog* DirFrame = new wxDirDialog( aParent,
                                             aTitle,
                                             aPath,
                                             aFlags,
                                             aPosition );

    ii = DirFrame->ShowModal();

    if( ii == wxID_OK )
    {
        aPath    = DirFrame->GetPath();
        selected = true;
    }

    DirFrame->Destroy();
    return selected;
}


wxString EDA_FILE_SELECTOR( const wxString& aTitle,
                            const wxString& aPath,
                            const wxString& aFileName,
                            const wxString& aExtension,
                            const wxString& aWildcard,
                            wxWindow*       aParent,
                            int             aStyle,
                            const bool      aKeepWorkingDirectory,
                            const wxPoint&  aPosition,
                            wxString*       aMruPath )
{
    wxString fullfilename;
    wxString curr_cwd    = wxGetCwd();
    wxString defaultname = aFileName;
    wxString defaultpath = aPath;
    wxString dotted_Ext = wxT(".") + aExtension;

#ifdef __WINDOWS__
    defaultname.Replace( wxT( "/" ), wxT( "\\" ) );
    defaultpath.Replace( wxT( "/" ), wxT( "\\" ) );
#endif

    if( defaultpath.IsEmpty() )
    {
        if( aMruPath == NULL )
            defaultpath = wxGetCwd();
        else
            defaultpath = *aMruPath;
    }

    wxSetWorkingDirectory( defaultpath );

#if 0 && defined (DEBUG)
    printf( "defaultpath=\"%s\" defaultname=\"%s\" Ext=\"%s\" Mask=\"%s\" flag=%d keep_working_directory=%d\n",
            TO_UTF8( defaultpath ),
            TO_UTF8( defaultname ),
            TO_UTF8( aExtension ),
            TO_UTF8( aWildcard ),
            aStyle,
            aKeepWorkingDirectory );
#endif

    fullfilename = wxFileSelector( aTitle,
                                   defaultpath,
                                   defaultname,
                                   dotted_Ext,
                                   aWildcard,
                                   aStyle,         // open mode wxFD_OPEN, wxFD_SAVE ..
                                   aParent,
                                   aPosition.x, aPosition.y );

    if( aKeepWorkingDirectory )
        wxSetWorkingDirectory( curr_cwd );

    if( !fullfilename.IsEmpty() && aMruPath )
    {
        wxFileName fn = fullfilename;
        *aMruPath = fn.GetPath();
    }

    return fullfilename;
}


wxString FindKicadFile( const wxString& shortname )
{
    // Test the presence of the file in the directory shortname of
    // the KiCad binary path.
#ifndef __WXMAC__
    wxString fullFileName = Pgm().GetExecutablePath() + shortname;
#else
    wxString fullFileName = Pgm().GetExecutablePath() + wxT( "Contents/MacOS/" ) + shortname;
#endif
    if( wxFileExists( fullFileName ) )
        return fullFileName;

    // Test the presence of the file in the directory shortname
    // defined by the environment variable KiCad.
    if( Pgm().IsKicadEnvVariableDefined() )
    {
        fullFileName = Pgm().GetKicadEnvVariable() + shortname;

        if( wxFileExists( fullFileName ) )
            return fullFileName;
    }

    // Path list for KiCad binary files
    const static wxChar* possibilities[] = {
#if defined( __WINDOWS__ )
        wxT( "c:/kicad/bin/" ),
        wxT( "d:/kicad/bin/" ),
        wxT( "c:/Program Files/kicad/bin/" ),
        wxT( "d:/Program Files/kicad/bin/" ),
#elif defined( __WXMAC__ )
        // all internal paths are relative to main bundle kicad.app
        wxT( "Contents/Applications/pcbnew.app/Contents/MacOS/" ),
        wxT( "Contents/Applications/eeschema.app/Contents/MacOS/" ),
        wxT( "Contents/Applications/gerbview.app/Contents/MacOS/" ),
        wxT( "Contents/Applications/bitmap2component.app/Contents/MacOS/" ),
        wxT( "Contents/Applications/pcb_calculator.app/Contents/MacOS/" ),
        wxT( "Contents/Applications/pl_editor.app/Contents/MacOS/" ),
#else
        wxT( "/usr/bin/" ),
        wxT( "/usr/local/bin/" ),
        wxT( "/usr/local/kicad/bin/" ),
#endif
    };

    // find binary file from possibilities list:
    for( unsigned i=0;  i<DIM(possibilities);  ++i )
    {
#ifndef __WXMAC__
        fullFileName = possibilities[i] + shortname;
#else
        // make relative paths absolute
        fullFileName = Pgm().GetExecutablePath() + possibilities[i] + shortname;
#endif

        if( wxFileExists( fullFileName ) )
            return fullFileName;
    }

    return shortname;
}


int ExecuteFile( wxWindow* frame, const wxString& ExecFile, const wxString& param,
                 wxProcess *callback )
{
    wxString fullFileName = FindKicadFile( ExecFile );

    if( wxFileExists( fullFileName ) )
    {
        if( !param.IsEmpty() )
            fullFileName += wxT( " " ) + param;

        return ProcessExecute( fullFileName, wxEXEC_ASYNC, callback );
    }
#ifdef __WXMAC__
    else
    {
        AddDelimiterString( fullFileName );

        if( !param.IsEmpty() )
            fullFileName += wxT( " " ) + param;

        return ProcessExecute( wxT( "/usr/bin/open -a " ) + fullFileName, wxEXEC_ASYNC, callback );
    }
#endif

    wxString msg;
    msg.Printf( _( "Command <%s> could not found" ), GetChars( fullFileName ) );
    DisplayError( frame, msg, 20 );
    return -1;
}


wxString KicadDatasPath()
{
    bool     found = false;
    wxString data_path;

    if( Pgm().IsKicadEnvVariableDefined() ) // Path defined by the KICAD environment variable.
    {
        data_path = Pgm().GetKicadEnvVariable();
        found = true;
    }
    else    // Path of executables.
    {
#ifndef __WXMAC__
        wxString tmp = Pgm().GetExecutablePath();
#ifdef __WINDOWS__
        tmp.MakeLower();
#endif
        if( tmp.Contains( wxT( "kicad" ) ) )
        {
#ifdef __WINDOWS__
            tmp = Pgm().GetExecutablePath();
#endif
            if( tmp.Last() == '/' )
                tmp.RemoveLast();

            data_path  = tmp.BeforeLast( '/' ); // id cd ../
            data_path += UNIX_STRING_DIR_SEP;

            // Old versions of KiCad use kicad/ as default for data
            // and last versions kicad/share/
            // So we search for kicad/share/ first
            wxString old_path = data_path;
            data_path += wxT( "share/" );

            if( wxDirExists( data_path ) )
            {
                found = true;
            }
            else if( wxDirExists( old_path ) )
            {
                data_path = old_path;
                found = true;
            }
        }
    }

    if( !found )
    {
        // find KiCad from possibilities list:
        //  /usr/local/kicad/ or c:/kicad/

        const static wxChar*  possibilities[] = {
#ifdef __WINDOWS__
            wxT( "c:/kicad/share/" ),
            wxT( "d:/kicad/share/" ),
            wxT( "c:/kicad/" ),
            wxT( "d:/kicad/" ),
            wxT( "c:/Program Files/kicad/share/" ),
            wxT( "d:/Program Files/kicad/share/" ),
            wxT( "c:/Program Files/kicad/" ),
            wxT( "d:/Program Files/kicad/" ),
#else
            wxT( "/usr/share/kicad/" ),
            wxT( "/usr/local/share/kicad/" ),
            wxT( "/usr/local/kicad/share/" ),   // default data path for "universal
                                                // tarballs" and build for a server
                                                // (new)
            wxT( "/usr/local/kicad/" ),         // default data path for "universal
                                                // tarballs" and build for a server
                                                // (old)
#endif
        };

        for( unsigned i=0;  i<DIM(possibilities);  ++i )
        {
            data_path = possibilities[i];

            if( wxDirExists( data_path ) )
            {
                found = true;
                break;
            }
        }
#else
        // On OSX point to Contents/SharedSupport folder of main bundle
        data_path = GetOSXKicadDataDir();
        found = true;
#endif
    }

    if( found )
    {
        data_path.Replace( WIN_STRING_DIR_SEP, UNIX_STRING_DIR_SEP );

        if( data_path.Last() != '/' )
            data_path += UNIX_STRING_DIR_SEP;
    }
    else
    {
        data_path.Empty();
    }

    return data_path;
}


bool OpenPDF( const wxString& file )
{
    wxString command;
    wxString filename = file;

    Pgm().ReadPdfBrowserInfos();

    if( !Pgm().UseSystemPdfBrowser() )    //  Run the preferred PDF Browser
    {
        AddDelimiterString( filename );
        command = Pgm().GetPdfBrowserName() + wxT( " " ) + filename;
    }
    else
    {
        wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( wxT( "pdf" ) );

        if( filetype )
            command = filetype->GetOpenCommand( filename );

        delete filetype;
    }

    if( !command.IsEmpty() )
    {
        if( ProcessExecute( command ) )
        {
            return true;
        }
        else
        {
            wxString msg;
            msg.Printf( _( "Problem while running the PDF viewer\nCommand is '%s'" ),
                        GetChars( command ) );
            DisplayError( NULL, msg );
        }
    }
    else
    {
        wxString msg;
        msg.Printf( _( "Unable to find a PDF viewer for <%s>" ), GetChars( filename ) );
        DisplayError( NULL, msg );
    }

    return false;
}


void OpenFile( const wxString& file )
{
    wxString    command;
    wxString    filename = file;

    wxFileName  CurrentFileName( filename );
    wxString    ext, type;

    ext = CurrentFileName.GetExt();
    wxFileType* filetype = wxTheMimeTypesManager->GetFileTypeFromExtension( ext );

    bool        success = false;

    wxFileType::MessageParameters params( filename, type );

    if( filetype )
        success = filetype->GetOpenCommand( &command, params );

    delete filetype;

    if( success && !command.IsEmpty() )
        ProcessExecute( command );
}


wxString QuoteFullPath( wxFileName& fn, wxPathFormat format )
{
    return wxT( "\"" ) + fn.GetFullPath( format ) + wxT( "\"" );
}