home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / shcutdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  4.9 KB  |  178 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. // Shortcut dialog implementation.   
  20. // 06/22/95 (jre)
  21.  
  22. #include "stdafx.h"
  23.  
  24. #include "shcut.h"
  25. #include "shcutdlg.h"
  26. #ifdef XP_WIN32
  27. #include <regstr.h>
  28. #include <shlobj.h>
  29. #endif
  30.  
  31. BEGIN_MESSAGE_MAP(CShortcutDialog, CDialog)
  32. END_MESSAGE_MAP()
  33.  
  34. // Internet shortcut class.  If a title is specified and it has no invalid
  35. // long filename characters, create "Shortcut to xxxx", where xxxx is the
  36. // specified title.  
  37.  
  38. CShortcutDialog::CShortcutDialog ( 
  39.     CWnd * pParentWnd, char * pszTitle,     // shortcut name
  40.     char * pszURL ) :                          // url to create shortcut to
  41.         CDialog ( IDD_INTERNETSHORTCUT, pParentWnd )
  42. {
  43.  
  44.     // invalid long filename characters
  45.     static char * invalidChars = ",\\/:*?<>|\"~";
  46.  
  47.     if ( pszURL )
  48.     {
  49.         unsigned pos;
  50.         char szNewURL[ _MAX_PATH ];
  51.  
  52.         // scan the title string to look for invalid long filename characters
  53.         for ( pos = 0; pos < strlen ( invalidChars ); pos++ )
  54.             if ( strchr ( pszTitle, invalidChars[ pos ] ) )
  55.             {
  56.  
  57.                 int i, j;
  58.                 for ( i = j = 0; i < CASTINT(strlen ( pszTitle )); i++ )
  59.                     if ( !strchr ( invalidChars, pszTitle[ i ] ) )
  60.                         szNewURL[ j++ ] = pszTitle[ i ];
  61.                 szNewURL[ j ] = '\0';
  62.                 break;
  63.             }
  64.  
  65.         // if no invalid long filename characters where found, use the
  66.         // title that was specified
  67.  
  68.         if ( pos == strlen ( invalidChars ) )
  69.             strcpy ( szNewURL, pszTitle );
  70.  
  71.         // create and move the shortcut name and url into the data transfer
  72.         // block for the MFC dialog
  73.         VERIFY(m_ShortcutName.LoadString(IDS_SHORTCUT_TO));
  74.         m_ShortcutName += ' ';
  75.         m_ShortcutName += szNewURL;
  76.         m_URL = CString(pszURL);
  77.     }
  78.     else
  79.         m_URL = _T("");
  80.  
  81.     m_Description = _T("");
  82. }
  83.  
  84.  
  85. // returns the shortcut name after the user has potentially edited it
  86.  
  87. const char * CShortcutDialog::GetDescription ( ) 
  88.     const char * ptr = (const char*)m_Description;
  89.     return ptr;
  90. }
  91.  
  92.  
  93. // returns the URL 
  94.  
  95. const char * CShortcutDialog::GetURL ( ) 
  96. {
  97.     const char * ptr = (const char *)m_URL;
  98.     return ptr;
  99. }
  100.  
  101. // transfers data from the dialog into our local class
  102.  
  103. void CShortcutDialog::DoDataExchange ( CDataExchange * pDX )
  104. {
  105.     CDialog::DoDataExchange(pDX);
  106.     DDX_Text(pDX,  IDC_INTERNETSHORTCUTLINK, m_URL);
  107.     DDX_Text(pDX,  IDC_INTERNETSHORTCUTDESCRIPTION, m_ShortcutName);
  108. }
  109.  
  110.  
  111. // initializes the dialog to contain the data which is initially specified
  112. // in our dialog class
  113.  
  114. BOOL CShortcutDialog::OnInitDialog ()
  115. {
  116.     CDialog::OnInitDialog();
  117.     CEdit * pShortcut = (CEdit *) GetDlgItem(IDC_INTERNETSHORTCUTLINK); 
  118.     if(pShortcut) 
  119.         pShortcut->SetWindowText((const char *)m_URL);
  120.     CEdit * pDescription = (CEdit *)GetDlgItem(IDC_INTERNETSHORTCUTDESCRIPTION);
  121.     if ( pDescription )
  122.     {
  123.         // sets shortcut name as the initial field with focus
  124.         pDescription->SetWindowText((const char *)m_ShortcutName);
  125.         pDescription->SetFocus();
  126.         pDescription->SetSel(0, -1);
  127.         return (0);
  128.     }
  129.  
  130.     return(1);
  131.     
  132. }
  133.  
  134. // when user selects the ok button, transfer the data from the controls into
  135. // our local class and create a valid shortcut long filename.
  136.  
  137. void CShortcutDialog::OnOK ( )
  138. {
  139.  
  140. #if defined(XP_WIN32)
  141.  
  142.     char szFilename[_MAX_PATH];
  143.     DWORD   cbData;
  144.     HKEY    hKey;
  145.  
  146.     UpdateData ( );        // transfer data from fields to class members
  147.  
  148.     // if user has chosen the "place on desktop" flag, create a .URL file 
  149.     // name on the desktop. Note that we should use the registry to find
  150.     // the path
  151.     if (RegOpenKeyEx(HKEY_CURRENT_USER, REGSTR_PATH_SPECIAL_FOLDERS, NULL, KEY_QUERY_VALUE, &hKey) != ERROR_SUCCESS)
  152.         return;  // not much we can do in this case
  153.  
  154.     cbData = sizeof(szFilename);
  155.     RegQueryValueEx(hKey, "Desktop", NULL, NULL, (LPBYTE)szFilename, &cbData);
  156.     RegCloseKey(hKey);
  157.  
  158.     // Make sure the path ends with a '\'
  159.     CString    strPath = szFilename;
  160.  
  161.     if (strPath.GetLength() == 0)
  162.         return;  // nothing we can do with this path
  163.  
  164.     if (strPath[strPath.GetLength() - 1] != '\\')
  165.         strPath += '\\';
  166.  
  167.     // Add the shortcut title and append the .URL extension
  168.     strPath += m_ShortcutName + ".URL";
  169.  
  170.     // assign the new description to the internal data member
  171.     m_Description = strPath;
  172.  
  173. #endif /* XP_WIN32 */
  174.  
  175.     CDialog::OnOK ( );
  176. }
  177.