]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.cpp
2c3f3262fd1fa0277c9d34e8e514448ce881966d
[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 #include "support/FileName.h"
18 #include "support/lstrings.h"
19
20 #ifdef __APPLE__
21 #include <Carbon/Carbon.h>
22 #endif
23
24 using namespace std;
25
26 namespace lyx {
27 namespace support {
28 namespace os {
29
30 void init(int, char *[])
31 {}
32
33
34 string current_root()
35 {
36         return "/";
37 }
38
39
40 bool isFilesystemCaseSensitive()
41 {
42 #ifdef __APPLE__
43         return false;
44 #else
45         return true;
46 #endif
47 }
48
49
50 docstring::size_type common_path(docstring const & p1, docstring const & p2)
51 {
52         docstring::size_type i = 0;
53         docstring::size_type const p1_len = p1.length();
54         docstring::size_type const p2_len = p2.length();
55         while (i < p1_len && i < p2_len && p1[i] == p2[i])
56                 ++i;
57         if ((i < p1_len && i < p2_len)
58             || (i < p1_len && p1[i] != '/' && i == p2_len)
59             || (i < p2_len && p2[i] != '/' && i == p1_len))
60         {
61                 if (i)
62                         --i;     // here was the last match
63                 while (i && p1[i] != '/')
64                         --i;
65         }
66         return i;
67 }
68
69
70 string external_path(string const & p)
71 {
72         return p;
73 }
74
75
76 string internal_path(string const & p)
77 {
78         return p;
79 }
80
81
82 string external_path_list(string const & p)
83 {
84         return p;
85 }
86
87
88 string internal_path_list(string const & p)
89 {
90         return p;
91 }
92
93
94 string latex_path(string const & p)
95 {
96         return p;
97 }
98
99
100 bool is_valid_strftime(string const & p)
101 {
102         string::size_type pos = p.find_first_of('%');
103         while (pos != string::npos) {
104                 if (pos + 1 == string::npos)
105                         break;
106                 if (!containsOnly(p.substr(pos + 1, 1),
107                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
108                         return false;
109                 if (pos + 2 == string::npos)
110                       break;
111                 pos = p.find_first_of('%', pos + 2);
112         }
113         return true;
114 }
115
116
117 char const * popen_read_mode()
118 {
119         return "r";
120 }
121
122
123 string const & nulldev()
124 {
125         static string const nulldev_ = "/dev/null";
126         return nulldev_;
127 }
128
129
130 shell_type shell()
131 {
132         return UNIX;
133 }
134
135
136 char path_separator()
137 {
138         return ':';
139 }
140
141
142 void windows_style_tex_paths(bool)
143 {}
144
145 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
146 {
147 #ifdef __APPLE__
148 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
149         CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
150                                         (UInt8 *) ext.c_str(), ext.length(),
151                                         kCFStringEncodingISOLatin1, false);
152         // this is what we would like to do but it seems that the
153         // viewer for PDF is often quicktime...
154         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
155         (void)mode;
156         LSRolesMask role = kLSRolesAll;
157         FSRef outAppRef;
158         OSStatus status =
159                 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
160                                         cfs_ext, role, &outAppRef, NULL);
161         CFRelease(cfs_ext);
162
163         return status != kLSApplicationNotFoundErr;
164 #else
165         // silence compiler warnings
166         (void)ext;
167         (void)mode;
168
169         // currently, no default viewer is tried for non-windows system
170         // support for KDE/Gnome/Macintosh may be added later
171         return false;
172 #endif
173 }
174
175
176 bool autoOpenFile(string const & filename, auto_open_mode const mode)
177 {
178 #ifdef __APPLE__
179 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
180         FSRef fileref;
181         OSStatus status =
182                 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
183         if (status != 0)
184                 return false;
185
186         // this is what we would like to do but it seems that the
187         // viewer for PDF is often quicktime...
188         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
189         (void)mode;
190         LSRolesMask role = kLSRolesAll;
191         FSRef outAppRef;
192
193         status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
194         if (status == kLSApplicationNotFoundErr)
195                 return false;
196
197         LSLaunchFSRefSpec inLaunchSpec;
198         inLaunchSpec.appRef = &outAppRef;
199         inLaunchSpec.numDocs = 1;
200         inLaunchSpec.itemRefs = &fileref;
201         inLaunchSpec.passThruParams = NULL;
202         inLaunchSpec.launchFlags = kLSLaunchDefaults;
203         inLaunchSpec.asyncRefCon = NULL;
204         status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
205
206         return status != kLSApplicationNotFoundErr;
207 #else
208         // silence compiler warnings
209         (void)filename;
210         (void)mode;
211
212         // currently, no default viewer is tried for non-windows system
213         // support for KDE/Gnome/Macintosh may be added later
214         return false;
215 #endif
216 }
217
218 } // namespace os
219 } // namespace support
220 } // namespace lyx