source: releases/ICeCoffEE/1.3.1/ICeCoffEE/ICeCoffEEActionMenu.c@ 285

Last change on this file since 285 was 88, checked in by Nicholas Riley, 21 years ago

ICeCoffEE 1.3.1d1

File size: 11.4 KB
Line 
1/*
2 * ICeCoffEEActionMenu.c
3 * ICeCoffEE APE
4 *
5 * Created by Nicholas Riley on Wed Jan 29 2003.
6 * Copyright (c) 2003 Nicholas Riley. All rights reserved.
7 *
8 */
9
10#include "ICeCoffEEActionMenu.h"
11#include "ICeCoffEEConfig.h"
12#include "ICeCoffEEShared.h"
13#include "ICeCoffEEBookmarks.h"
14
15/* thanks to Slava Karpenko for this one! */
16OSStatus _LSCopyApplicationURLsForItemURL(CFURLRef inURL, LSRolesMask inRoleMask, CFArrayRef *outApps); // outApps to be CFReleased()
17
18#define THROW_ERR(e) { err = e; goto END; }
19
20static CFStringRef ICCF_NameWithLocation(CFStringRef name, CFURLRef url) {
21 CFURLRef urlMinus = CFURLCreateCopyDeletingLastPathComponent(NULL, url);
22 CFStringRef urlString = CFURLCopyFileSystemPath(urlMinus, kCFURLPOSIXPathStyle);
23 CFStringRef nameWithLocation = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@ %s %@"), name, "Ñ", urlString);
24
25 SAFE_RELEASE(urlMinus);
26 SAFE_RELEASE(urlString);
27 return nameWithLocation;
28}
29
30static const MenuItemIndex kICAppMenuItemHasPath = 0xfffe;
31
32typedef struct {
33 CFURLRef defaultAppURL; // URL of default app, set to NULL after added to menu
34 CFMutableSetRef appPaths;
35 CFMutableDictionaryRef appItemTitles; // keys: URLs; values: item titles (CFString)
36 CFMutableDictionaryRef appURLs; // keys: app display names (CFString), values: URL of item without path appended, or NULL if already appended (at least 2 items with this name exist)
37 MenuRef menu;
38} icAppMenuContext;
39
40static OSStatus ICCF_AddAppItemTitle(icAppMenuContext *ctx, CFURLRef appURL) {
41 CFStringRef appName = NULL, appItemTitle = NULL;
42 CFBundleRef appBundle = NULL;
43 OSStatus err = noErr;
44
45 CFStringRef appPath = CFURLCopyPath(appURL);
46 // only one entry for each path
47 if (CFSetContainsValue(ctx->appPaths, appPath))
48 return dupFNErr;
49 CFSetAddValue(ctx->appPaths, appPath);
50
51 if ( (err = LSCopyDisplayNameForURL(appURL, &appName)) != noErr)
52 return err;
53
54 // if we encounter multiple applications with the same display name, add locations to the menu item titles to disambiguate them
55 CFURLRef sameAppURL;
56 Boolean shouldAppendLocation;
57 if ( (shouldAppendLocation = CFDictionaryGetValueIfPresent(ctx->appURLs, appName, (const void **)&sameAppURL)) && (CFTypeRef)sameAppURL != kCFNull) {
58 // this app is the second encountered with the same name; go back and fix the menu item title of the first app
59 CFStringRef sameAppItemTitle = CFDictionaryGetValue(ctx->appItemTitles, sameAppURL);
60 CFStringRef appItemTitleWithVersion = ICCF_NameWithLocation(sameAppItemTitle, sameAppURL);
61 CFDictionarySetValue(ctx->appItemTitles, sameAppURL, appItemTitleWithVersion);
62 SAFE_RELEASE(appItemTitleWithVersion);
63 CFDictionarySetValue(ctx->appURLs, appName, kCFNull);
64 }
65
66 CFRetain(appName);
67 appItemTitle = appName;
68
69 if ( (appBundle = CFBundleCreate(NULL, appURL)) != NULL) {
70 // prefer a short version string, e.g. "1.0 Beta" instead of "51" for Safari
71 CFStringRef appVersion = CFBundleGetValueForInfoDictionaryKey(appBundle, CFSTR("CFBundleShortVersionString"));
72 if (appVersion == NULL)
73 appVersion = CFBundleGetValueForInfoDictionaryKey(appBundle, kCFBundleVersionKey);
74 if (appVersion != NULL) {
75 appItemTitle = CFStringCreateWithFormat(NULL, NULL, CFSTR("%@ (%@)"), appName, appVersion);
76 CFRelease(appName);
77 }
78 CFRelease(appBundle);
79 }
80
81 if (shouldAppendLocation) {
82 CFStringRef appItemTitleWithVersion = ICCF_NameWithLocation(appItemTitle, appURL);
83 CFRelease(appItemTitle);
84 appItemTitle = appItemTitleWithVersion;
85 } else {
86 CFDictionarySetValue(ctx->appURLs, appName, appURL);
87 }
88
89 if (ctx->defaultAppURL != NULL && CFEqual(appURL, ctx->defaultAppURL)) {
90 CFStringRef defaultFormat = ICCF_CopyLocalizedString(CFSTR("DefaultApp%@"));
91 CFStringRef appItemTitleWithDefault = CFStringCreateWithFormat(NULL, NULL, defaultFormat, appItemTitle);
92 CFRelease(appItemTitle);
93 appItemTitle = appItemTitleWithDefault;
94 ctx->defaultAppURL = NULL; // mark as added
95 }
96
97 CFDictionarySetValue(ctx->appItemTitles, appURL, appItemTitle);
98 CFRelease(appItemTitle);
99
100 return noErr;
101}
102
103static OSStatus ICCF_AddTitledAppToMenu(icAppMenuContext *ctx, CFURLRef appURL, MenuCommand menuCommand) {
104 CFStringRef appItemTitle = NULL;
105 IconRef appIcon = NULL;
106 FSRef appFSR;
107 SInt16 label;
108 MenuItemIndex menuItemIndex;
109 OSStatus err = noErr;
110
111 appItemTitle = CFDictionaryGetValue(ctx->appItemTitles, appURL);
112 if (appItemTitle == NULL) return fnfErr;
113
114 err = AppendMenuItemTextWithCFString(ctx->menu, appItemTitle, 0, 0, &menuItemIndex);
115 if (err != noErr) return err;
116
117 if (!CFURLGetFSRef(appURL, &appFSR)) return paramErr;
118 err = GetIconRefFromFileInfo(&appFSR, 0, NULL, kFSCatInfoNone, NULL, kIconServicesNormalUsageFlag, &appIcon, &label);
119 if (err != noErr) return err;
120
121 SetMenuItemIconHandle(ctx->menu, menuItemIndex, kMenuIconRefType, (Handle)appIcon);
122 SetMenuItemCommandID(ctx->menu, menuItemIndex, menuCommand);
123 SetMenuItemRefCon(ctx->menu, menuItemIndex, (UInt32)appURL);
124 ReleaseIconRef(appIcon);
125
126 return err;
127}
128
129static OSStatus ICCF_AddAppToMenu(icAppMenuContext *ctx, CFURLRef appURL, MenuCommand menuCommand) {
130 OSStatus err = ICCF_AddAppItemTitle(ctx, appURL);
131 switch (err) {
132 case noErr: break;
133 case dupFNErr: return noErr;
134 default: return err;
135 }
136 return ICCF_AddTitledAppToMenu(ctx, appURL, menuCommand);
137}
138
139CFComparisonResult ICCF_CompareURLsByItemTitle(const void *url1, const void *url2, void *appItemTitles) {
140 CFStringRef appItemTitle1 = CFDictionaryGetValue((CFDictionaryRef)appItemTitles, (CFURLRef)url1);
141 CFStringRef appItemTitle2 = CFDictionaryGetValue((CFDictionaryRef)appItemTitles, (CFURLRef)url2);
142 return CFStringCompareWithOptions(appItemTitle1, appItemTitle2,
143 CFRangeMake(0, CFStringGetLength(appItemTitle1)),
144 kCFCompareCaseInsensitive | kCFCompareNumerically);
145}
146
147enum {
148 kICURLActionOpenWith = 'OpnW',
149 kICURLActionAddBookmark = 'AddB'
150};
151
152OSStatus ICCF_DoURLActionMenu(ICInstance inst, ConstStr255Param hint, const char *urlData, long startIndex, long endIndex) {
153 Handle h = NewHandle(0);
154 CFURLRef url = NULL;
155 CFArrayRef appURLsUnsorted = NULL; // matching app URLs
156 CFMutableArrayRef appURLs = NULL; // matching app URLs sorted by item title
157 CFArrayRef urlArray = NULL; // single-URL array
158 icAppMenuContext ctx = {NULL, NULL, NULL, NULL};
159 OSStatus err;
160
161 if (h == NULL) return MemError();
162
163 if ( (err = ICParseURL(inst, hint, urlData + startIndex, endIndex - startIndex + 1,
164 &startIndex, &endIndex, h)) != noErr) THROW_ERR(err);
165
166 if ( (url = CFURLCreateWithBytes(NULL, *h, GetHandleSize(h), kCFStringEncodingASCII,
167 NULL)) == NULL) THROW_ERR(paramErr);
168
169 if ( (err = _LSCopyApplicationURLsForItemURL(url, kLSRolesAll, &appURLsUnsorted)) != noErr)
170 THROW_ERR(err);
171
172 CFIndex appCount = 0;
173 if (appURLsUnsorted == NULL || (appCount = CFArrayGetCount(appURLsUnsorted)) == 0)
174 THROW_ERR(kLSApplicationNotFoundErr);
175
176 if ( (appURLs = CFArrayCreateMutableCopy(NULL, appCount, appURLsUnsorted)) == NULL)
177 THROW_ERR(memFullErr);
178
179 if ( (ctx.appPaths = CFSetCreateMutable(NULL, appCount, &kCFCopyStringSetCallBacks)) == NULL)
180 THROW_ERR(memFullErr);
181
182 if ( (ctx.appItemTitles = CFDictionaryCreateMutable(NULL, appCount, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)) == NULL)
183 THROW_ERR(memFullErr);
184
185 if ( (ctx.appURLs = CFDictionaryCreateMutable(NULL, appCount, &kCFCopyStringDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)) == NULL)
186 THROW_ERR(memFullErr);
187
188 LSGetApplicationForURL(url, kLSRolesAll, NULL, &ctx.defaultAppURL);
189
190 CFIndex appIndex;
191 CFURLRef appURL;
192 for (appIndex = 0 ; appIndex < appCount ; appIndex++) {
193 appURL = CFArrayGetValueAtIndex(appURLs, appIndex);
194 err = ICCF_AddAppItemTitle(&ctx, appURL);
195 switch (err) {
196 case noErr: break;
197 case dupFNErr:
198 CFArrayRemoveValueAtIndex(appURLs, appIndex);
199 appIndex--;
200 appCount--;
201 break;
202 default:
203 THROW_ERR(err);
204 }
205 }
206
207 CFArraySortValues(appURLs, CFRangeMake(0, appCount), ICCF_CompareURLsByItemTitle, ctx.appItemTitles);
208
209 if ( (err = CreateNewMenu(0, kMenuAttrExcludesMarkColumn, &ctx.menu)) != noErr)
210 THROW_ERR(err);
211
212 MenuItemIndex menuItemIndex;
213 if ( (err = AppendMenuItemTextWithCFString(ctx.menu, ICCF_CopyLocalizedString(CFSTR("Open Location With")), kMenuItemAttrDisabled, 0, &menuItemIndex)) != noErr)
214 THROW_ERR(err);
215
216 if ( (urlArray = CFArrayCreate(NULL, (const void **)&url, 1, &kCFTypeArrayCallBacks)) == NULL)
217 THROW_ERR(memFullErr);
218
219 for (appIndex = 0 ; appIndex < appCount ; appIndex++) {
220 appURL = CFArrayGetValueAtIndex(appURLs, appIndex);
221
222 if ( (err = ICCF_AddTitledAppToMenu(&ctx, appURL, kICURLActionOpenWith)) != noErr)
223 THROW_ERR(err);
224 }
225 // sometimes the default protocol handler won't be on the list because it doesnÕt claim to handle that protocol; add it anyway
226 if (ctx.defaultAppURL != NULL) {
227 if ( (err = ICCF_AddAppToMenu(&ctx, ctx.defaultAppURL, kICURLActionOpenWith)) != noErr)
228 THROW_ERR(err);
229 }
230
231 appURL = ICCF_GetBookmarkHelperURL(inst);
232 if (appURL != NULL) {
233 if ( (err = AppendMenuItemTextWithCFString(ctx.menu, CFSTR(""), kMenuItemAttrSeparator, 0, NULL)) != noErr)
234 THROW_ERR(err);
235 if ( (err = AppendMenuItemTextWithCFString(ctx.menu, ICCF_CopyLocalizedString(CFSTR("Add Bookmark")), kMenuItemAttrDisabled, 0, &menuItemIndex)) != noErr)
236 THROW_ERR(err);
237 // the app won't show up at the bottom if someone cmd-option-clicks on 'bookmark:', but they'd have to be crazy to do that anyway
238 if ( (err = ICCF_AddAppToMenu(&ctx, appURL, kICURLActionAddBookmark)) != noErr)
239 THROW_ERR(err);
240 }
241
242 InsertMenu(ctx.menu, -1);
243 Point mousePoint;
244 GetGlobalMouse(&mousePoint);
245 long selectedAppIndex = PopUpMenuSelect(ctx.menu, mousePoint.v + 18, mousePoint.h - 30, 0/*popUpItem*/);
246 if (selectedAppIndex == 0) {
247 err = userCanceledErr;
248 } else {
249 CFURLRef appURL = NULL;
250 MenuCommand menuCommand;
251 err = GetMenuItemRefCon(ctx.menu, selectedAppIndex, (void *)&appURL);
252 if (err == noErr) {
253 err = GetMenuItemCommandID(ctx.menu, selectedAppIndex, &menuCommand);
254 if (err == noErr) {
255 if (menuCommand == kICURLActionOpenWith) {
256 LSLaunchURLSpec lsSpec = {appURL, urlArray, NULL, kLSLaunchDefaults};
257 err = LSOpenFromURLSpec(&lsSpec, NULL);
258 } else if (menuCommand == kICURLActionAddBookmark) {
259 err = ICCF_DoBookmarkDialog(inst, CFURLGetString(url));
260 }
261 }
262 }
263 }
264
265END:
266 if (h != NULL) DisposeHandle(h);
267 if (ctx.menu != NULL) {
268 DeleteMenu(GetMenuID(ctx.menu));
269 DisposeMenu(ctx.menu);
270 }
271 SAFE_RELEASE(url);
272 SAFE_RELEASE(urlArray);
273 SAFE_RELEASE(appURLsUnsorted);
274 SAFE_RELEASE(appURLs);
275 SAFE_RELEASE(ctx.appPaths);
276 SAFE_RELEASE(ctx.appItemTitles);
277 SAFE_RELEASE(ctx.appURLs);
278
279 return err;
280}
Note: See TracBrowser for help on using the repository browser.