]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.cpp
transfer os::is_absolute_path() to FileName::isAbsolute().
[lyx.git] / src / support / os_unix.cpp
1 /**
2  * \file os_unix.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Ruurd A. Reitsma
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Various OS specific functions
11  */
12
13 #include <config.h>
14
15 #include "support/os.h"
16 #include "support/docstring.h"
17
18 #ifdef __APPLE__
19 #include <Carbon/Carbon.h>
20 #endif
21
22 using namespace std;
23
24 namespace lyx {
25 namespace support {
26 namespace os {
27
28 void init(int, char *[])
29 {}
30
31
32 string current_root()
33 {
34         return "/";
35 }
36
37
38 docstring::size_type common_path(docstring const & p1, docstring const & p2)
39 {
40         docstring::size_type i = 0;
41         docstring::size_type const p1_len = p1.length();
42         docstring::size_type const p2_len = p2.length();
43         while (i < p1_len && i < p2_len && p1[i] == p2[i])
44                 ++i;
45         if ((i < p1_len && i < p2_len)
46             || (i < p1_len && p1[i] != '/' && i == p2_len)
47             || (i < p2_len && p2[i] != '/' && i == p1_len))
48         {
49                 if (i)
50                         --i;     // here was the last match
51                 while (i && p1[i] != '/')
52                         --i;
53         }
54         return i;
55 }
56
57
58 string external_path(string const & p)
59 {
60         return p;
61 }
62
63
64 string internal_path(string const & p)
65 {
66         return p;
67 }
68
69
70 string external_path_list(string const & p)
71 {
72         return p;
73 }
74
75
76 string internal_path_list(string const & p)
77 {
78         return p;
79 }
80
81
82 string latex_path(string const & p)
83 {
84         return p;
85 }
86
87
88 char const * popen_read_mode()
89 {
90         return "r";
91 }
92
93
94 string const & nulldev()
95 {
96         static string const nulldev_ = "/dev/null";
97         return nulldev_;
98 }
99
100
101 shell_type shell()
102 {
103         return UNIX;
104 }
105
106
107 char path_separator()
108 {
109         return ':';
110 }
111
112
113 void windows_style_tex_paths(bool)
114 {}
115
116 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
117 {
118 #ifdef __APPLE__
119 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
120         CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
121                                         (UInt8 *) ext.c_str(), ext.length(),
122                                         kCFStringEncodingISOLatin1, false);
123         // this is what we would like to do but it seems that the
124         // viewer for PDF is often quicktime...
125         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
126         (void)mode;
127         LSRolesMask role = kLSRolesAll;
128         FSRef outAppRef;
129         OSStatus status =
130                 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
131                                         cfs_ext, role, &outAppRef, NULL);
132         CFRelease(cfs_ext);
133
134         return status != kLSApplicationNotFoundErr;
135 #else
136         // silence compiler warnings
137         (void)ext;
138         (void)mode;
139
140         // currently, no default viewer is tried for non-windows system
141         // support for KDE/Gnome/Macintosh may be added later
142         return false;
143 #endif
144 }
145
146
147 bool autoOpenFile(string const & filename, auto_open_mode const mode)
148 {
149 #ifdef __APPLE__
150 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
151         FSRef fileref;
152         OSStatus status =
153                 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
154         if (status != 0)
155                 return false;
156
157         // this is what we would like to do but it seems that the
158         // viewer for PDF is often quicktime...
159         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
160         (void)mode;
161         LSRolesMask role = kLSRolesAll;
162         FSRef outAppRef;
163
164         status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
165         if (status == kLSApplicationNotFoundErr)
166                 return false;
167
168         LSLaunchFSRefSpec inLaunchSpec;
169         inLaunchSpec.appRef = &outAppRef;
170         inLaunchSpec.numDocs = 1;
171         inLaunchSpec.itemRefs = &fileref;
172         inLaunchSpec.passThruParams = NULL;
173         inLaunchSpec.launchFlags = kLSLaunchDefaults;
174         inLaunchSpec.asyncRefCon = NULL;
175         status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
176
177         return status != kLSApplicationNotFoundErr;
178 #else
179         // silence compiler warnings
180         (void)filename;
181         (void)mode;
182
183         // currently, no default viewer is tried for non-windows system
184         // support for KDE/Gnome/Macintosh may be added later
185         return false;
186 #endif
187 }
188
189 } // namespace os
190 } // namespace support
191 } // namespace lyx