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