changed: update version strings for beta4
[xbmc.git] / xbmc / utils / AliasShortcutUtils.cpp
blob265ed3d0f49071dae04656c5a7b042fe98b0659c
1 /*
2 * Copyright (C) 2009-2010 Team XBMC
3 * http://www.xbmc.org
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
8 * any later version.
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, write to
17 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
18 * http://www.gnu.org/copyleft/gpl.html
22 #if defined(__APPLE__)
23 #include <CoreServices/CoreServices.h>
24 #elif defined(_LINUX)
25 #else
26 #endif
28 #include "Util.h"
29 #include "AliasShortcutUtils.h"
31 bool IsAliasShortcut(CStdString &path)
33 bool rtn = false;
35 #if defined(__APPLE__)
36 // Note: regular files that have an .alias extension can be
37 // reported as an alias when clearly, they are not. Trap them out.
38 if (CUtil::GetExtension(path) != ".alias")
40 FSRef fileRef;
41 Boolean targetIsFolder, wasAliased;
43 // It is better to call FSPathMakeRefWithOptions and pass kFSPathMakeRefDefaultOptions
44 // since it will succeed for paths such as "/Volumes" unlike FSPathMakeRef.
45 if (noErr == FSPathMakeRefWithOptions((UInt8*)path.c_str(), kFSPathMakeRefDefaultOptions, &fileRef, NULL))
47 if (noErr == FSIsAliasFile(&fileRef, &wasAliased, &targetIsFolder))
49 if (wasAliased)
51 rtn = true;
56 #elif defined(_LINUX)
57 // Linux does not use alias or shortcut methods
58 #else
59 /* Needs testing under Windows platform so ignore shortcuts for now
60 if (CUtil::GetExtension(path) == ".lnk")
62 rtn = true;
65 #endif
66 return(rtn);
69 void TranslateAliasShortcut(CStdString &path)
71 #if defined(__APPLE__)
72 FSRef fileRef;
73 Boolean targetIsFolder, wasAliased;
75 if (noErr == FSPathMakeRefWithOptions((UInt8*)path.c_str(), kFSPathMakeRefDefaultOptions, &fileRef, NULL))
77 if (noErr == FSResolveAliasFileWithMountFlags(&fileRef, TRUE, &targetIsFolder, &wasAliased, kResolveAliasFileNoUI))
79 if (wasAliased)
81 char real_path[PATH_MAX];
82 if (noErr == FSRefMakePath(&fileRef, (UInt8*)real_path, PATH_MAX))
84 path = real_path;
89 #elif defined(_LINUX)
90 // Linux does not use alias or shortcut methods
92 #else
93 /* Needs testing under Windows platform so ignore shortcuts for now
94 CComPtr<IShellLink> ipShellLink;
96 // Get a pointer to the IShellLink interface
97 if (NOERROR == CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (void**)&ipShellLink))
98 WCHAR wszTemp[MAX_PATH];
100 // Get a pointer to the IPersistFile interface
101 CComQIPtr<IPersistFile> ipPersistFile(ipShellLink);
103 // IPersistFile is using LPCOLESTR so make sure that the string is Unicode
104 #if !defined _UNICODE
105 MultiByteToWideChar(CP_ACP, 0, lpszShortcutPath, -1, wszTemp, MAX_PATH);
106 #else
107 wcsncpy(wszTemp, lpszShortcutPath, MAX_PATH);
108 #endif
110 // Open the shortcut file and initialize it from its contents
111 if (NOERROR == ipPersistFile->Load(wszTemp, STGM_READ))
113 // Try to find the target of a shortcut even if it has been moved or renamed
114 if (NOERROR == ipShellLink->Resolve(NULL, SLR_UPDATE))
116 WIN32_FIND_DATA wfd;
117 TCHAR real_path[PATH_MAX];
118 // Get the path to the shortcut target
119 if (NOERROR == ipShellLink->GetPath(real_path, MAX_PATH, &wfd, SLGP_RAWPATH))
121 // Get the description of the target
122 TCHAR szDesc[MAX_PATH];
123 if (NOERROR == ipShellLink->GetDescription(szDesc, MAX_PATH))
125 path = real_path;
132 #endif