< prev index next >

src/java.desktop/windows/native/libawt/windows/ShellFolder2.cpp

Print this page




  47 #include "alloc.h"
  48 
  49 #include "stdhdrs.h"
  50 
  51 // Copy from shlguid.h which is no longer in PlatformSDK
  52 #ifndef DEFINE_SHLGUID
  53 #define DEFINE_SHLGUID(name, l, w1, w2) DEFINE_GUID(name, l, w1, w2, 0xC0, 0, 0, 0, 0, 0, 0, 0x46)
  54 #endif
  55 
  56 // {93F2F68C-1D1B-11d3-A30E-00C04F79ABD1}
  57 DEFINE_GUID(IID_IShellFolder2, 0x93f2f68c, 0x1d1b, 0x11d3, 0xa3, 0xe, 0x0, 0xc0, 0x4f, 0x79, 0xab, 0xd1);
  58 
  59 #undef IID_IShellLinkW
  60 #undef IID_IExtractIconW
  61 // copied from shlguid.h
  62 DEFINE_SHLGUID(IID_IShellLinkW,         0x000214F9L, 0, 0);
  63 DEFINE_SHLGUID(IID_IExtractIconW,       0x000214FAL, 0, 0);
  64 
  65 //#include <sun_awt_shell_Win32ShellFolder2.h>
  66 










  67 // Shell Functions
  68 typedef BOOL (WINAPI *DestroyIconType)(HICON);
  69 typedef HINSTANCE (WINAPI *FindExecutableType)(LPCTSTR,LPCTSTR,LPTSTR);
  70 typedef HICON (WINAPI *ImageList_GetIconType)(HIMAGELIST,int,UINT);
  71 typedef BOOL (WINAPI *GetIconInfoType)(HICON,PICONINFO);
  72 typedef HRESULT (WINAPI *SHGetDesktopFolderType)(IShellFolder**);
  73 typedef DWORD* (WINAPI *SHGetFileInfoType)(LPCTSTR,DWORD,SHFILEINFO*,UINT,UINT);
  74 typedef HRESULT (WINAPI *SHGetMallocType)(IMalloc**);
  75 typedef BOOL (WINAPI *SHGetPathFromIDListType)(LPCITEMIDLIST,LPTSTR);
  76 typedef HRESULT (WINAPI *SHGetSpecialFolderLocationType)(HWND,int,LPITEMIDLIST*);
  77 
  78 static DestroyIconType fn_DestroyIcon;
  79 static FindExecutableType fn_FindExecutable;
  80 static GetIconInfoType fn_GetIconInfo;
  81 static ImageList_GetIconType fn_ImageList_GetIcon;
  82 static SHGetDesktopFolderType fn_SHGetDesktopFolder;
  83 static SHGetFileInfoType fn_SHGetFileInfo;
  84 static SHGetMallocType fn_SHGetMalloc;
  85 static SHGetPathFromIDListType fn_SHGetPathFromIDList;
  86 static SHGetSpecialFolderLocationType fn_SHGetSpecialFolderLocation;


1246     Java_sun_awt_shell_Win32ShellFolder2_compareIDsByColumn
1247             (JNIEnv* env, jclass cls, jlong jpParentIShellFolder,
1248             jlong pIDL1, jlong pIDL2, jint columnIdx)
1249 {
1250     IShellFolder* pParentIShellFolder = (IShellFolder*)jpParentIShellFolder;
1251     if (pParentIShellFolder == NULL) {
1252         return 0;
1253     }
1254 
1255     HRESULT hr = pParentIShellFolder->CompareIDs(
1256                                             (UINT) columnIdx,
1257                                             (LPCITEMIDLIST) pIDL1,
1258                                             (LPCITEMIDLIST) pIDL2);
1259     if (SUCCEEDED (hr)) {
1260         return (jint) (short) HRESULT_CODE(hr);
1261     }
1262 
1263     return 0;
1264 }
1265 



















































































































































































































1266 
1267 } // extern "C"


  47 #include "alloc.h"
  48 
  49 #include "stdhdrs.h"
  50 
  51 // Copy from shlguid.h which is no longer in PlatformSDK
  52 #ifndef DEFINE_SHLGUID
  53 #define DEFINE_SHLGUID(name, l, w1, w2) DEFINE_GUID(name, l, w1, w2, 0xC0, 0, 0, 0, 0, 0, 0, 0x46)
  54 #endif
  55 
  56 // {93F2F68C-1D1B-11d3-A30E-00C04F79ABD1}
  57 DEFINE_GUID(IID_IShellFolder2, 0x93f2f68c, 0x1d1b, 0x11d3, 0xa3, 0xe, 0x0, 0xc0, 0x4f, 0x79, 0xab, 0xd1);
  58 
  59 #undef IID_IShellLinkW
  60 #undef IID_IExtractIconW
  61 // copied from shlguid.h
  62 DEFINE_SHLGUID(IID_IShellLinkW,         0x000214F9L, 0, 0);
  63 DEFINE_SHLGUID(IID_IExtractIconW,       0x000214FAL, 0, 0);
  64 
  65 //#include <sun_awt_shell_Win32ShellFolder2.h>
  66 
  67 #define DEFINE_FIELD_ID(var, cls, field, type)                            \
  68     jfieldID var = env->GetFieldID(cls, field, type);                     \
  69     DASSERT(var != NULL);                                                 \
  70     CHECK_NULL_RETURN(var, NULL);
  71 
  72 #define EXCEPTION_CHECK                                                   \
  73    if(env->ExceptionCheck()) {                                            \
  74         throw std::bad_alloc();                                           \
  75    }
  76 
  77 // Shell Functions
  78 typedef BOOL (WINAPI *DestroyIconType)(HICON);
  79 typedef HINSTANCE (WINAPI *FindExecutableType)(LPCTSTR,LPCTSTR,LPTSTR);
  80 typedef HICON (WINAPI *ImageList_GetIconType)(HIMAGELIST,int,UINT);
  81 typedef BOOL (WINAPI *GetIconInfoType)(HICON,PICONINFO);
  82 typedef HRESULT (WINAPI *SHGetDesktopFolderType)(IShellFolder**);
  83 typedef DWORD* (WINAPI *SHGetFileInfoType)(LPCTSTR,DWORD,SHFILEINFO*,UINT,UINT);
  84 typedef HRESULT (WINAPI *SHGetMallocType)(IMalloc**);
  85 typedef BOOL (WINAPI *SHGetPathFromIDListType)(LPCITEMIDLIST,LPTSTR);
  86 typedef HRESULT (WINAPI *SHGetSpecialFolderLocationType)(HWND,int,LPITEMIDLIST*);
  87 
  88 static DestroyIconType fn_DestroyIcon;
  89 static FindExecutableType fn_FindExecutable;
  90 static GetIconInfoType fn_GetIconInfo;
  91 static ImageList_GetIconType fn_ImageList_GetIcon;
  92 static SHGetDesktopFolderType fn_SHGetDesktopFolder;
  93 static SHGetFileInfoType fn_SHGetFileInfo;
  94 static SHGetMallocType fn_SHGetMalloc;
  95 static SHGetPathFromIDListType fn_SHGetPathFromIDList;
  96 static SHGetSpecialFolderLocationType fn_SHGetSpecialFolderLocation;


1256     Java_sun_awt_shell_Win32ShellFolder2_compareIDsByColumn
1257             (JNIEnv* env, jclass cls, jlong jpParentIShellFolder,
1258             jlong pIDL1, jlong pIDL2, jint columnIdx)
1259 {
1260     IShellFolder* pParentIShellFolder = (IShellFolder*)jpParentIShellFolder;
1261     if (pParentIShellFolder == NULL) {
1262         return 0;
1263     }
1264 
1265     HRESULT hr = pParentIShellFolder->CompareIDs(
1266                                             (UINT) columnIdx,
1267                                             (LPCITEMIDLIST) pIDL1,
1268                                             (LPCITEMIDLIST) pIDL2);
1269     if (SUCCEEDED (hr)) {
1270         return (jint) (short) HRESULT_CODE(hr);
1271     }
1272 
1273     return 0;
1274 }
1275 
1276 /*
1277  * Class:     sun_awt_shell_Win32ShellFolder2
1278  * Method:    loadKnownFolders
1279  * Signature: (V)[BLsun/awt/shell/Win32ShellFolder2$KnownfolderDefenition;
1280  */
1281 JNIEXPORT jobjectArray JNICALL Java_sun_awt_shell_Win32ShellFolder2_loadKnownFolders
1282     (JNIEnv* env, jclass cls )
1283 {
1284     CoInitialize(NULL);
1285     IKnownFolderManager* pkfm = NULL;
1286     HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL,
1287                                 CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));
1288     if (!SUCCEEDED(hr)) return NULL;
1289 
1290     TRY;
1291 
1292     jclass cl = env->FindClass("sun/awt/shell/Win32ShellFolder2$KnownFolderDefinition");
1293     CHECK_NULL_RETURN(cl, NULL);
1294     DEFINE_FIELD_ID(field_guid, cl, "guid", "Ljava/lang/String;")
1295     DEFINE_FIELD_ID(field_name, cl, "name", "Ljava/lang/String;");
1296     DEFINE_FIELD_ID(field_description, cl, "description", "Ljava/lang/String;");
1297     DEFINE_FIELD_ID(field_parent, cl, "parent", "Ljava/lang/String;");
1298     DEFINE_FIELD_ID(field_relativePath, cl, "relativePath", "Ljava/lang/String;");
1299     DEFINE_FIELD_ID(field_parsingName, cl, "parsingName", "Ljava/lang/String;");
1300     DEFINE_FIELD_ID(field_tooltip, cl, "tooltip", "Ljava/lang/String;");
1301     DEFINE_FIELD_ID(field_localizedName, cl, "localizedName", "Ljava/lang/String;");
1302     DEFINE_FIELD_ID(field_icon, cl, "icon", "Ljava/lang/String;");
1303     DEFINE_FIELD_ID(field_security, cl, "security", "Ljava/lang/String;");
1304     DEFINE_FIELD_ID(field_path, cl, "path", "Ljava/lang/String;");
1305     DEFINE_FIELD_ID(field_saveLocation, cl, "saveLocation", "Ljava/lang/String;");
1306     DEFINE_FIELD_ID(field_category, cl, "category", "I");
1307     DEFINE_FIELD_ID(field_attributes, cl, "attributes", "J");
1308     DEFINE_FIELD_ID(field_defenitionFlags, cl, "defenitionFlags", "I");
1309     DEFINE_FIELD_ID(field_ftidType, cl, "ftidType", "Ljava/lang/String;");
1310 
1311     jobjectArray result;
1312         KNOWNFOLDERID* pFoldersIds = NULL;
1313         UINT count = 0;
1314         if (SUCCEEDED(pkfm->GetFolderIds(&pFoldersIds, &count))) {
1315             jmethodID initMethod;
1316             try {
1317             result = env->NewObjectArray(count, cl, NULL);
1318             initMethod = env->GetMethodID(cl, "<init>", "()V");
1319             EXCEPTION_CHECK
1320         } catch (std::bad_alloc&) {
1321             CoTaskMemFree(pFoldersIds);
1322             pkfm->Release();
1323             throw;
1324         }
1325         for(UINT i = 0; i < count; ++i)
1326         {
1327             jobject fld;
1328             const KNOWNFOLDERID& folderId = pFoldersIds[i];
1329             LPOLESTR guid = NULL;
1330                 try {
1331                 fld = env->NewObject(cl, initMethod);
1332                 if (fld) {
1333                     env->SetObjectArrayElement(result, i, fld);
1334                 }
1335                 EXCEPTION_CHECK
1336 
1337                 if (SUCCEEDED(StringFromCLSID(folderId, &guid))) {
1338                     jstring jstr = JNU_NewStringPlatform(env, guid);
1339                     if (jstr) {
1340                         env->SetObjectField(fld, field_guid, jstr);
1341                     }
1342                     CoTaskMemFree(guid);
1343                     EXCEPTION_CHECK
1344                 }
1345             } catch (std::bad_alloc&) {
1346                 CoTaskMemFree(pFoldersIds);
1347                 pkfm->Release();
1348                 throw;
1349             }
1350 
1351             IKnownFolder* pFolder = NULL;
1352             if (SUCCEEDED(pkfm->GetFolder(folderId, &pFolder))) {
1353                 KNOWNFOLDER_DEFINITION kfDef;
1354                 if (SUCCEEDED(pFolder->GetFolderDefinition(&kfDef)))
1355                 {
1356                     try {
1357                         jstring jstr = JNU_NewStringPlatform(env, kfDef.pszName);
1358                         if(jstr) {
1359                             env->SetObjectField(fld, field_name, jstr);
1360                         }
1361                         EXCEPTION_CHECK
1362                         if (kfDef.pszDescription) {
1363                             jstr = JNU_NewStringPlatform(env, kfDef.pszDescription);
1364                             if (jstr) {
1365                                 env->SetObjectField(fld, field_description, jstr);
1366                             }
1367                             EXCEPTION_CHECK
1368                         }
1369                         EXCEPTION_CHECK
1370                         if (SUCCEEDED(StringFromCLSID(kfDef.fidParent, &guid))) {
1371                             jstr = JNU_NewStringPlatform(env, guid);
1372                             if (jstr) {
1373                                 env->SetObjectField(fld, field_parent, jstr);
1374                             }
1375                             CoTaskMemFree(guid);
1376                             EXCEPTION_CHECK
1377                         }
1378                         if (kfDef.pszRelativePath) {
1379                             jstr = JNU_NewStringPlatform(env, kfDef.pszRelativePath);
1380                             if (jstr) {
1381                                 env->SetObjectField(fld, field_relativePath, jstr);
1382                             }
1383                             EXCEPTION_CHECK
1384                         }
1385                         if (kfDef.pszParsingName) {
1386                             jstr = JNU_NewStringPlatform(env, kfDef.pszParsingName);
1387                             if (jstr) {
1388                                 env->SetObjectField(fld, field_parsingName, jstr);
1389                             }
1390                             EXCEPTION_CHECK
1391                         }
1392                         if (kfDef.pszTooltip) {
1393                             jstr = JNU_NewStringPlatform(env, kfDef.pszTooltip);
1394                             if (jstr) {
1395                                 env->SetObjectField(fld, field_tooltip, jstr);
1396                             }
1397                             EXCEPTION_CHECK
1398                         }
1399                         if (kfDef.pszLocalizedName) {
1400                             jstr = JNU_NewStringPlatform(env, kfDef.pszLocalizedName);
1401                             if (jstr) {
1402                                 env->SetObjectField(fld, field_localizedName, jstr);
1403                             }
1404                             EXCEPTION_CHECK
1405                         }
1406                         if (kfDef.pszIcon) {
1407                             jstr = JNU_NewStringPlatform(env, kfDef.pszIcon);
1408                             if (jstr) {
1409                                 env->SetObjectField(fld, field_icon, jstr);
1410                             }
1411                             EXCEPTION_CHECK
1412                         }
1413                         if (kfDef.pszSecurity) {
1414                             jstr = JNU_NewStringPlatform(env, kfDef.pszSecurity);
1415                             if (jstr) {
1416                                 env->SetObjectField(fld, field_security, jstr);
1417                             }
1418                             EXCEPTION_CHECK
1419                         }
1420                         if (SUCCEEDED(StringFromCLSID(kfDef.ftidType, &guid))) {
1421                             jstr = JNU_NewStringPlatform(env, guid);
1422                             if (jstr) {
1423                                 env->SetObjectField(fld, field_ftidType, jstr);
1424                             }
1425                             CoTaskMemFree(guid);
1426                             EXCEPTION_CHECK
1427                         }
1428                         env->SetIntField(fld, field_category, kfDef.category);
1429                         env->SetIntField(fld, field_defenitionFlags, kfDef.kfdFlags);
1430                         env->SetLongField(fld, field_attributes, kfDef.dwAttributes);
1431                     
1432                         LPWSTR folderPath = NULL;
1433                         if (SUCCEEDED(pFolder->GetPath(KF_FLAG_NO_ALIAS, &folderPath))
1434                                     && folderPath) {
1435                             jstr = JNU_NewStringPlatform(env, folderPath);
1436                             if (jstr) {
1437                                 env->SetObjectField(fld, field_path, jstr);
1438                             }
1439                             CoTaskMemFree(folderPath);
1440                             EXCEPTION_CHECK
1441                         }
1442     
1443                         IShellLibrary *plib = NULL;
1444                         hr = CoCreateInstance(CLSID_ShellLibrary, NULL,
1445                                          CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&plib));
1446                         if (SUCCEEDED(hr)) {
1447                             hr = plib->LoadLibraryFromKnownFolder(folderId, STGM_READWRITE);
1448                             if (SUCCEEDED(hr)) {
1449                                 IShellItem *item = NULL;
1450                                 hr = plib->GetDefaultSaveFolder(DSFT_DETECT,
1451                                         IID_PPV_ARGS(&item));
1452                                 if (SUCCEEDED(hr) && item) {
1453                                     LPWSTR loc = NULL;
1454                                     hr = item->GetDisplayName(SIGDN_FILESYSPATH, &loc);
1455                                     if (SUCCEEDED(hr) && loc)
1456                                     {
1457                                         jstr = JNU_NewStringPlatform(env, loc);
1458                                         if (jstr) {
1459                                             env->SetObjectField(fld, field_saveLocation, jstr);
1460                                         }
1461                                         CoTaskMemFree(loc);
1462                                     }
1463                                     item->Release();
1464                                 }
1465                             }
1466                             plib->Release();
1467                             EXCEPTION_CHECK
1468                         }
1469                         FreeKnownFolderDefinitionFields(&kfDef);                        
1470                     } catch (std::bad_alloc&) {
1471                         FreeKnownFolderDefinitionFields(&kfDef);
1472                         pFolder->Release();
1473                         CoTaskMemFree(pFoldersIds);
1474                         pkfm->Release();
1475                         throw;
1476                     }
1477                 }
1478             }
1479             pFolder->Release();
1480         }
1481         CoTaskMemFree(pFoldersIds);
1482         }
1483     pkfm->Release();
1484     return result;
1485     CATCH_BAD_ALLOC_RET(NULL);
1486 }
1487 
1488 } // extern "C"
< prev index next >