summaryrefslogtreecommitdiff
path: root/lib/ffs_paths.cpp
blob: 5ee4a3ebea42ec453f2bc65d2bf7b14fe0638ff7 (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
// **************************************************************************
// * This file is part of the FreeFileSync project. It is distributed under *
// * GNU General Public License: http://www.gnu.org/licenses/gpl.html       *
// * Copyright (C) Zenju (zenju AT gmx DOT de) - All Rights Reserved        *
// **************************************************************************

#include "ffs_paths.h"
#include <zen/file_handling.h>
#include <wx/stdpaths.h>
#include <wx+/string_conv.h>

#ifdef FFS_MAC
#include <vector>
#include <zen/scope_guard.h>
#include <zen/osx_string.h>
//keep in .cpp file to not pollute global namespace! e.g. with UInt64:
#include <ApplicationServices/ApplicationServices.h> //LSFindApplicationForInfo
#endif

using namespace zen;


namespace
{
#if defined FFS_WIN || defined FFS_LINUX
inline
Zstring getExecutableDir() //directory containing executable WITH path separator at end
{
    return appendSeparator(beforeLast(utfCvrtTo<Zstring>(wxStandardPaths::Get().GetExecutablePath()), FILE_NAME_SEPARATOR));
}
#endif

#ifdef FFS_WIN
inline
Zstring getInstallDir() //root install directory WITH path separator at end
{
    return appendSeparator(beforeLast(beforeLast(getExecutableDir(), FILE_NAME_SEPARATOR), FILE_NAME_SEPARATOR));
}
#endif


#ifdef FFS_WIN
inline
bool isPortableVersion() { return !fileExists(getInstallDir() + L"uninstall.exe"); } //this check is a bit lame...
#elif defined FFS_LINUX
inline
bool isPortableVersion() { return !endsWith(getExecutableDir(), "/bin/"); } //this check is a bit lame...
#endif
}


bool zen::manualProgramUpdateRequired()
{
#if defined FFS_WIN || defined FFS_MAC
    return true;
#elif defined FFS_LINUX
    return isPortableVersion(); //locally installed version is updated by system
#endif
}


Zstring zen::getResourceDir()
{
#ifdef FFS_WIN
    return getInstallDir();
#elif defined FFS_LINUX
    if (isPortableVersion())
        return getExecutableDir();
    else //use OS' standard paths
        return appendSeparator(toZ(wxStandardPathsBase::Get().GetResourcesDir()));
#elif defined FFS_MAC
    return appendSeparator(toZ(wxStandardPathsBase::Get().GetResourcesDir())); //if packaged, used "Contents/Resources", else the executable directory
#endif
}


Zstring zen::getConfigDir()
{
#ifdef FFS_WIN
    if (isPortableVersion())
        return getInstallDir();
#elif defined FFS_LINUX
    if (isPortableVersion())
        return getExecutableDir();
#elif defined FFS_MAC
    //portable apps do not seem common on OS - fine with me: http://theocacao.com/document.page/319
#endif
    //use OS' standard paths
    Zstring userDirectory = toZ(wxStandardPathsBase::Get().GetUserDataDir());

    if (!dirExists(userDirectory))
        try
        {
            makeDirectory(userDirectory); //throw FileError
        }
        catch (const FileError&) {}

    return appendSeparator(userDirectory);
}


//this function is called by RealtimeSync!!!
Zstring zen::getFreeFileSyncLauncher()
{
#ifdef FFS_WIN
    return getInstallDir() + Zstr("FreeFileSync.exe");

#elif defined FFS_LINUX
    return getExecutableDir() + Zstr("FreeFileSync");

#elif defined FFS_MAC
    CFURLRef appURL = nullptr;
    ZEN_ON_SCOPE_EXIT(if (appURL) ::CFRelease(appURL));

    if (::LSFindApplicationForInfo(kLSUnknownCreator, // OSType inCreator,
                                   CFSTR("net.SourceForge.FreeFileSync"),//CFStringRef inBundleID,
                                   nullptr,           //CFStringRef inName,
                                   nullptr,           //FSRef *outAppRef,
                                   &appURL) == noErr) //CFURLRef *outAppURL
        if (appURL)
            if (CFURLRef absUrl = ::CFURLCopyAbsoluteURL(appURL))
            {
                ZEN_ON_SCOPE_EXIT(::CFRelease(absUrl));

                if (CFStringRef path = ::CFURLCopyFileSystemPath(absUrl, kCFURLPOSIXPathStyle))
                {
                    ZEN_ON_SCOPE_EXIT(::CFRelease(path));
                    return appendSeparator(osx::cfStringToZstring(path)) + "Contents/MacOS/FreeFileSync";
                }
            }
    return Zstr("./FreeFileSync"); //fallback: at least give some hint...
#endif
}
bgstack15