]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.cpp
* get rid of support::absolutePath()
[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
19 #ifdef __APPLE__
20 #include <Carbon/Carbon.h>
21 #endif
22
23 using namespace std;
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 char const * popen_read_mode()
90 {
91         return "r";
92 }
93
94
95 string const & nulldev()
96 {
97         static string const nulldev_ = "/dev/null";
98         return nulldev_;
99 }
100
101
102 shell_type shell()
103 {
104         return UNIX;
105 }
106
107
108 char path_separator()
109 {
110         return ':';
111 }
112
113
114 void windows_style_tex_paths(bool)
115 {}
116
117 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
118 {
119 #ifdef __APPLE__
120 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
121         CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
122                                         (UInt8 *) ext.c_str(), ext.length(),
123                                         kCFStringEncodingISOLatin1, false);
124         // this is what we would like to do but it seems that the
125         // viewer for PDF is often quicktime...
126         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
127         (void)mode;
128         LSRolesMask role = kLSRolesAll;
129         FSRef outAppRef;
130         OSStatus status =
131                 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
132                                         cfs_ext, role, &outAppRef, NULL);
133         CFRelease(cfs_ext);
134
135         return status != kLSApplicationNotFoundErr;
136 #else
137         // silence compiler warnings
138         (void)ext;
139         (void)mode;
140
141         // currently, no default viewer is tried for non-windows system
142         // support for KDE/Gnome/Macintosh may be added later
143         return false;
144 #endif
145 }
146
147
148 bool autoOpenFile(string const & filename, auto_open_mode const mode)
149 {
150 #ifdef __APPLE__
151 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
152         FSRef fileref;
153         OSStatus status =
154                 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
155         if (status != 0)
156                 return false;
157
158         // this is what we would like to do but it seems that the
159         // viewer for PDF is often quicktime...
160         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
161         (void)mode;
162         LSRolesMask role = kLSRolesAll;
163         FSRef outAppRef;
164
165         status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
166         if (status == kLSApplicationNotFoundErr)
167                 return false;
168
169         LSLaunchFSRefSpec inLaunchSpec;
170         inLaunchSpec.appRef = &outAppRef;
171         inLaunchSpec.numDocs = 1;
172         inLaunchSpec.itemRefs = &fileref;
173         inLaunchSpec.passThruParams = NULL;
174         inLaunchSpec.launchFlags = kLSLaunchDefaults;
175         inLaunchSpec.asyncRefCon = NULL;
176         status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
177
178         return status != kLSApplicationNotFoundErr;
179 #else
180         // silence compiler warnings
181         (void)filename;
182         (void)mode;
183
184         // currently, no default viewer is tried for non-windows system
185         // support for KDE/Gnome/Macintosh may be added later
186         return false;
187 #endif
188 }
189
190 } // namespace os
191 } // namespace support
192 } // namespace lyx