Reset inheritable flag of debug log file git_shell_ext_debug.txt
[git-cheetah/kirill.git] / finder / util.c
blob08f757db865097f703c31034e518dfb0b2d288c3
1 /* util.c
3 * Implementation of a finder menu plugin for git-cheetah
5 * This provides Mac OS X specific utility functions
7 * Copyright (C) Heiko Voigt, 2009
9 * inspired by an example from Brent Simmons
10 * brent@ranchero.com, http://macte.ch/kmyXM
13 #include "../common/git-compat-util.h"
14 #include <dirent.h>
15 #include <unistd.h>
16 #include <sys/wait.h>
17 #include "../common/strbuf.h"
18 #include "../common/debug.h"
19 #include "../common/exec.h"
20 #include "plugin.h"
21 #include "util.h"
23 #define MAX_ARGS 32
25 const char *git_path()
27 return "";
30 void message_box(const char *string)
32 CFStringRef msg = CFStringCreateWithCString(NULL,
33 string, kCFStringEncodingASCII);
35 CFUserNotificationDisplayNotice(0,
36 kCFUserNotificationNoteAlertLevel,
37 NULL, NULL, NULL, CFSTR("Git Cheetah says"),
38 msg, CFSTR("Ok"));
41 int selection_to_path(char *path, int len, const AEDesc *selection)
43 AEDesc selected_item = { typeNull, NULL };
44 int status = 1;
45 Size item_size;
47 AliasHandle item_data_handle;
48 FSRef file_ref;
49 Boolean changed = false;
51 if ((!get_file_from_selection(selection, &selected_item)) &&
52 (!get_file_from_selection_list(selection, &selected_item))) {
53 debug_git("failure get_file_from_*");
54 return 0;
57 item_size = AEGetDescDataSize(&selected_item);
59 item_data_handle = (AliasHandle) NewHandle(item_size);
60 if (item_data_handle == nil) {
61 status = 0;
62 goto selection_to_path_fail;
65 if (AEGetDescData(&selected_item, *item_data_handle, item_size) != noErr) {
66 status = 0;
67 goto selection_to_path_fail;
70 if (FSResolveAlias (NULL, item_data_handle, &file_ref, &changed) != noErr) {
71 status = 0;
72 goto selection_to_path_fail;
75 if (FSRefMakePath(&file_ref, (UInt8 *) path, len) != noErr)
76 status = 0;
78 selection_to_path_fail:
79 DisposeHandle((Handle) item_data_handle);
80 AEDisposeDesc(&selected_item);
81 return status;
85 /* if this selection is a file return it, if not NULL is returned */
86 AEDesc *get_file_from_selection(const AEDesc *selection, AEDesc *file)
88 AEDesc temp = { typeNull, NULL };
90 if (selection->descriptorType == typeAlias) {
91 if (AEDuplicateDesc(selection, file) != noErr)
92 return NULL;
93 return file;
96 if (AECoerceDesc(selection, typeAlias, &temp) != noErr)
97 goto get_file_from_selection_fail;
99 if (temp.descriptorType != typeAlias)
100 goto get_file_from_selection_fail;
102 if (AEDuplicateDesc(&temp, file) != noErr)
103 goto get_file_from_selection_fail;
105 /* TODO: check if this is a memory leak */
106 return file;
108 get_file_from_selection_fail:
109 AEDisposeDesc(&temp);
110 return NULL;
113 AEDesc *get_file_from_selection_list(const AEDesc *selection, AEDesc *file
114 /* , int *index */)
116 AEDesc temp = { typeNull, NULL };
117 AEDesc *result;
118 AEKeyword dummy;
119 long count;
121 if (selection->descriptorType != typeAEList)
122 return NULL;
124 if (AECountItems(selection, &count) != noErr)
125 return NULL;
127 if (count != 1)
128 return NULL;
130 if (AEGetNthDesc(selection, 1, typeWildCard, &dummy, &temp) != noErr)
131 return NULL;
133 result = get_file_from_selection(&temp, file);
134 AEDisposeDesc(&temp);
136 return result;