sbKnownFolderManager.cpp
Go to the documentation of this file.
1 /*
2  *=BEGIN SONGBIRD GPL
3  *
4  * This file is part of the Songbird web player.
5  *
6  * Copyright(c) 2005-2010 POTI, Inc.
7  * http://www.songbirdnest.com
8  *
9  * This file may be licensed under the terms of of the
10  * GNU General Public License Version 2 (the ``GPL'').
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the GPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the GPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/gpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  *=END SONGBIRD GPL
23  */
24 
25 #include "sbKnownFolderManager.h"
26 
27 // Mozilla Includes
28 #include <nsILocalFile.h>
29 #include <nsStringGlue.h>
30 
31 // Windows Includes
32 #if defined(_WIN32_WINNT)
33  #undef _WIN32_WINNT
34  #define _WIN32_WINNT 0x0600 //Windows Vista or Greater
35 #endif
36 
37 #include <ShObjIdl.h>
38 
39 #define SB_WIN_ENSURE_SUCCESS(aHR, aRV) \
40  PR_BEGIN_MACRO \
41  if(FAILED(aHR)) { \
42  return aRV; \
43  } \
44  PR_END_MACRO
45 
48 
50 {
51 }
52 
53 sbKnownFolderManager::~sbKnownFolderManager()
54 {
55 }
56 
57 nsresult
59 {
60  // Warn, don't fail, on RPC_E_CHANGED_MODE
61  HRESULT hr = mCOMInit.Initialize(COINIT_MULTITHREADED);
62  NS_ENSURE_TRUE(SUCCEEDED(hr) || hr == RPC_E_CHANGED_MODE, NS_ERROR_FAILURE);
63  NS_WARN_IF_FALSE(SUCCEEDED(hr), "RPC_E_CHANGED_MODE on COM initialization");
64 
65  nsRefPtr<IKnownFolderManager> knownFolderManager;
66  hr = ::CoCreateInstance(CLSID_KnownFolderManager,
67  NULL,
68  CLSCTX_INPROC_SERVER,
69  IID_IKnownFolderManager,
70  getter_AddRefs(knownFolderManager));
72 
73  knownFolderManager.swap(mKnownFolderManager);
74 
75  return NS_OK;
76 }
77 
78 nsresult
79 sbKnownFolderManager::GetDefaultDisplayName(const nsAString &aFolderPath,
80  nsAString &aDisplayName)
81 {
82  nsCOMPtr<nsILocalFile> localFile;
83  nsresult rv = NS_NewLocalFile(aFolderPath,
84  PR_FALSE,
85  getter_AddRefs(localFile));
86  NS_ENSURE_SUCCESS(rv, rv);
87 
88  rv = localFile->GetLeafName(aDisplayName);
89  NS_ENSURE_SUCCESS(rv, rv);
90 
91  return NS_OK;
92 }
93 
94 NS_IMETHODIMP
95 sbKnownFolderManager::GetDisplayNameFromPath(const nsAString &aFolderPath,
96  nsAString &aDisplayName)
97 {
98  // Get the default return value in case we error out.
99  nsresult rv = GetDefaultDisplayName(aFolderPath, aDisplayName);
100  NS_ENSURE_SUCCESS(rv, rv);
101 
102  //
103  // First, we cover the case where we are using a Windows Version
104  // that is lower than Windows Vista.
105  //
106 
107  if(!mKnownFolderManager) {
108  // Just return since we already set aDisplayName to a default value.
109  return NS_OK;
110  }
111 
112  //
113  // Second, we cover the case where we are using a Window Version
114  // that is at least Windows Vista.
115  //
116 
117  nsRefPtr<IKnownFolder> knownFolder;
118  HRESULT hr =
119  mKnownFolderManager->FindFolderFromPath(aFolderPath.BeginReading(),
120  FFFP_EXACTMATCH,
121  getter_AddRefs(knownFolder));
123 
124  nsRefPtr<IShellItem> shellItem;
125  hr = knownFolder->GetShellItem(0, IID_IShellItem, getter_AddRefs(shellItem));
127 
128  LPWSTR displayName = NULL;
129  hr = shellItem->GetDisplayName(SIGDN_NORMALDISPLAY, &displayName);
131 
132  nsDependentString tempStr(displayName);
133  aDisplayName.Assign(tempStr);
134 
135  ::CoTaskMemFree(displayName);
136 
137  return NS_OK;
138 }
return NS_OK
HRESULT Initialize(DWORD aInitFlags=COINIT_MULTITHREADED)
sbAutoCOMInitializer mCOMInit
nsRefPtr< IKnownFolderManager > mKnownFolderManager
Manage Known Operating System Folders.
NS_DECL_ISUPPORTS NS_DECL_SBIKNOWNFOLDERMANAGER sbKnownFolderManager()
NS_IMPL_ISUPPORTS1(sbKnownFolderManager, sbIKnownFolderManager)
#define SB_WIN_ENSURE_SUCCESS(aHR, aRV)
nsresult GetDefaultDisplayName(const nsAString &aFolderPath, nsAString &aDisplayName)