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