[FONTEXT] Fix font installation 3846/head
authorMark Jansen <mark.jansen@reactos.org>
Sat, 24 Jul 2021 23:11:01 +0000 (01:11 +0200)
committerMark Jansen <mark.jansen@reactos.org>
Mon, 15 Nov 2021 19:02:14 +0000 (20:02 +0100)
dll/shellext/fontext/CFontCache.hpp
dll/shellext/fontext/CFontExt.cpp

index e047e6b..ba9d9e3 100644 (file)
@@ -45,9 +45,10 @@ protected:
     CFontCache();
 
     void Insert(CAtlList<CFontInfo>& fonts, const CStringW& KeyName);
-    void Read();
 
 public:
+    void Read();
+
     void SetFontDir(const LPCWSTR Path);
     const CStringW& FontPath() const { return m_FontFolderPath; }
 
index 23b7e0b..7fffa90 100644 (file)
@@ -603,10 +603,15 @@ STDMETHODIMP CFontExt::Drop(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt,
         }
     }
 
-    // TODO: update g_FontCache
+    // Invalidate our cache
+    g_FontCache->Read();
 
+    // Notify the system that a font was added
     SendMessageW(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
 
+    // Notify the shell that the folder contents are changed
+    SHChangeNotify(SHCNE_UPDATEDIR, SHCNF_PATHW, g_FontCache->FontPath().GetString(), NULL);
+
     // TODO: Show message
 
     return bOK ? S_OK : E_FAIL;
@@ -615,15 +620,30 @@ STDMETHODIMP CFontExt::Drop(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt,
 HRESULT CFontExt::DoInstallFontFile(LPCWSTR pszFontPath, LPCWSTR pszFontsDir, HKEY hkeyFonts)
 {
     WCHAR szDestFile[MAX_PATH];
-    LPCWSTR pszFileTitle = PathFindFileName(pszFontPath);
 
-    CStringW strFontName;
-    if (!DoGetFontTitle(pszFontPath, strFontName))
+    // Add this font to the font list, so we can query the name
+    if (!AddFontResourceW(pszFontPath))
+    {
+        ERR("AddFontResourceW('%S') failed\n", pszFontPath);
+        DeleteFileW(szDestFile);
         return E_FAIL;
+    }
+
+    CStringW strFontName;
+    HRESULT hr = DoGetFontTitle(pszFontPath, strFontName);
 
-    RemoveFontResourceW(pszFileTitle);
+    // We got the name, remove it again
+    RemoveFontResourceW(pszFontPath);
+
+    if (!SUCCEEDED(hr))
+    {
+        ERR("DoGetFontTitle failed (err=0x%x)!\n", hr);
+        return hr;
+    }
 
     StringCchCopyW(szDestFile, sizeof(szDestFile), pszFontsDir);
+
+    LPCWSTR pszFileTitle = PathFindFileName(pszFontPath);
     PathAppendW(szDestFile, pszFileTitle);
     if (!CopyFileW(pszFontPath, szDestFile, FALSE))
     {
@@ -631,24 +651,18 @@ HRESULT CFontExt::DoInstallFontFile(LPCWSTR pszFontPath, LPCWSTR pszFontsDir, HK
         return E_FAIL;
     }
 
-    if (!AddFontResourceW(szDestFile))
-    {
-        ERR("AddFontResourceW('%S') failed\n", pszFileTitle);
-        DeleteFileW(szDestFile);
-        return E_FAIL;
-    }
-
     DWORD cbData = (wcslen(pszFileTitle) + 1) * sizeof(WCHAR);
     LONG nError = RegSetValueExW(hkeyFonts, strFontName, 0, REG_SZ,
                                  (const BYTE *)pszFileTitle, cbData);
     if (nError)
     {
         ERR("RegSetValueExW failed with %ld\n", nError);
-        RemoveFontResourceW(pszFileTitle);
         DeleteFileW(szDestFile);
         return E_FAIL;
     }
 
+    AddFontResourceW(szDestFile);
+
     return S_OK;
 }
 
@@ -659,12 +673,13 @@ CFontExt::DoGetFontTitle(IN LPCWSTR pszFontPath, OUT CStringW& strFontName)
     BOOL ret = GetFontResourceInfoW(pszFontPath, &cbInfo, NULL, 1);
     if (!ret || !cbInfo)
     {
-        ERR("GetFontResourceInfoW failed\n");
+        ERR("GetFontResourceInfoW failed (err: %u)\n", GetLastError());
         return E_FAIL;
     }
 
     LPWSTR pszBuffer = strFontName.GetBuffer(cbInfo / sizeof(WCHAR));
     ret = GetFontResourceInfoW(pszFontPath, &cbInfo, pszBuffer, 1);
+    DWORD dwErr = GetLastError();;
     strFontName.ReleaseBuffer();
     if (ret)
     {
@@ -672,6 +687,6 @@ CFontExt::DoGetFontTitle(IN LPCWSTR pszFontPath, OUT CStringW& strFontName)
         return S_OK;
     }
 
-    ERR("GetFontResourceInfoW failed\n");
+    ERR("GetFontResourceInfoW failed (err: %u)\n", dwErr);
     return E_FAIL;
 }