home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / prefs / CPrefsDialog.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  13.6 KB  |  470 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. #include "CPrefsDialog.h"
  20.  
  21. #include "CMenuTable.h"
  22. #ifdef MOZ_MAIL_NEWS
  23. #include "MailNewsAddressBook.h"
  24. #endif
  25.  
  26. #include "MailNewsMediators.h"
  27. #include "CAssortedMediators.h"
  28.  
  29. #ifdef MOZ_MAIL_NEWS
  30. #include "CReceiptsMediator.h"
  31. #endif // MOZ_MAIL_NEWS
  32. #include "CSpecialFoldersMediator.h"
  33. #include "CMailNewsMainMediator.h"
  34.  
  35. #include "MPreference.h"
  36.  
  37. #include "UStdDialogs.h"
  38. #include "CValidEditField.h"
  39. #include "macutil.h"
  40.  
  41. #include "dirprefs.h"
  42. #include "addrbook.h"
  43. #include "abdefn.h"
  44. #include "prefapi.h"
  45.  
  46. #include "UStdDialogs.h"
  47.  
  48. #include "resgui.h"    // for cmd_AboutPlugins
  49.  
  50. #include "uapp.h"
  51.  
  52.  
  53.  
  54. CPrefsDialog *CPrefsDialog::sThis = nil;
  55.  
  56. //-----------------------------------
  57. CPrefsDialog::CPrefsDialog()
  58. //-----------------------------------
  59. :    LCommander(GetTopCommander())
  60. ,    mWindow(nil)
  61. ,    mTable(nil)
  62. {
  63.     MPreferenceBase::SetWriteOnDestroy(false);
  64. }
  65.  
  66. //-----------------------------------
  67. CPrefsDialog::~CPrefsDialog()
  68. //-----------------------------------
  69. {
  70.     // all panes are freed by PowerPlant since they are subcommanders of this dialog...
  71. }
  72.  
  73.  
  74. //-----------------------------------
  75. void CPrefsDialog::EditPrefs(
  76.     Expand_T expand,
  77.     PrefPaneID::ID pane,
  78.     Selection_T selection)
  79. //-----------------------------------
  80. {
  81.     if (sThis)
  82.     {
  83.         // very suspicious
  84.     }
  85.     else
  86.     {
  87.         sThis = new CPrefsDialog();
  88.     }
  89.     XP_Bool    attachVCard;
  90.     const    char *    const    usePABCPrefName = "mail.attach_vcard";    // fix me
  91.     int    prefResult = PREF_GetBoolPref(usePABCPrefName, &attachVCard);
  92. #ifdef MOZ_MAIL_NEWS
  93.     sThis->mNeedToCheckForVCard = (    prefResult == PREF_NOERROR &&
  94.                                     false == attachVCard &&
  95.                                     !PREF_PrefIsLocked(usePABCPrefName));
  96. #endif // MOZ_MAIL_NEWS
  97.     const    char *    const    useInternetConfigPrefName = "browser.mac.use_internet_config";    // fix me
  98.     XP_Bool    useIC;
  99.     prefResult = PREF_GetBoolPref(useInternetConfigPrefName, &useIC);
  100.     CPrefsMediator::UseIC(useIC);
  101.     sThis->DoPrefsWindow(expand, pane, selection);
  102. }
  103.  
  104. #ifdef MOZ_MAIL_NEWS
  105. //-----------------------------------
  106. void CPrefsDialog::CheckForVCard()
  107. //-----------------------------------
  108. {
  109.     const    char *    const    usePABCPrefName = "mail.attach_vcard";    // fix me
  110.     XP_Bool    attachVCard;
  111.     PREF_GetBoolPref(usePABCPrefName, &attachVCard);
  112.     if (attachVCard)
  113.     {
  114.         char    *email = (char *)FE_UsersMailAddress();
  115.         if (email)
  116.             email = XP_STRDUP(email);
  117.         char    *nameString = (char *)FE_UsersFullName();
  118.         if (nameString)
  119.         {
  120.             nameString = XP_STRDUP(nameString);
  121.         }
  122.         XP_List    *directories = CAddressBookManager::GetDirServerList();
  123.         DIR_Server    *pab;
  124.         DIR_GetPersonalAddressBook(directories, &pab);
  125.         XP_ASSERT(pab);
  126.         if (pab)
  127.         {
  128.             ABID        entryID;
  129.             PersonEntry    person;
  130.             person.Initialize();
  131.             person.pGivenName = nameString;
  132.             person.pEmailAddress = email;
  133.             ABook    *aBook = FE_GetAddressBook(nil);
  134.             AB_GetEntryIDForPerson(pab, aBook, &entryID, &person);
  135.             AB_BreakApartFirstName(aBook, &person);
  136.             if (MSG_MESSAGEIDNONE == entryID)
  137.             {
  138.                 LCommander    *super = LCommander::GetTopCommander();
  139.  
  140.                 StStdDialogHandler theHandler(12007, super);
  141.                 theHandler.SetInitialDialogPosition(nil);
  142.                 LWindow* theDialog = theHandler.GetDialog();
  143.  
  144.                 MessageT theMessage = msg_Cancel;
  145.                 if (UStdDialogs::TryToInteract())
  146.                 {
  147.                     theDialog->Show();
  148.                     theMessage = theHandler.WaitUserResponse();
  149.                 }
  150.                 if (msg_Cancel != theMessage)
  151.                 {
  152.                     if (true != FE_ShowPropertySheetFor(nil, entryID, &person))
  153.                     {
  154.                         theMessage = msg_Cancel;
  155.                     }
  156.                 }
  157.                 if (msg_Cancel == theMessage)
  158.                 {
  159.                     PREF_SetBoolPref(usePABCPrefName, false);
  160.                 }
  161.             }
  162.             person.CleanUp();
  163.         }
  164.     }
  165. }
  166. #endif // MOZ_MAIL_NEWS
  167.  
  168. //-----------------------------------
  169. void CPrefsDialog::DoPrefsWindow(
  170.     Expand_T expand,
  171.     PrefPaneID::ID pane,
  172.     Selection_T selection)
  173. //-----------------------------------
  174. {
  175.     if (!mWindow)
  176.     {
  177.         mCurrentMediator = nil;
  178.         mWindow = LWindow::CreateWindow(PrefPaneID::eWindowPPob, this);
  179.         if (mWindow)
  180.         {
  181.             UReanimator::LinkListenerToControls(    this,
  182.                                                     mWindow,
  183.                                                     PrefPaneID::eWindowPPob);
  184.             mTable = (CMenuTable *)mWindow->FindPaneByID(eTableView);
  185.             XP_ASSERT(mTable);
  186.             mPanel = (LView *)mWindow->FindPaneByID(ePanelView);
  187.             XP_ASSERT(mPanel);
  188.             CPrefsMediator::SetStatics(mPanel, mWindow, selection);
  189.  
  190.             mTable->AddListener(this);
  191.             
  192.             PrefPaneID::ID panel = pane;
  193.             if ( ! pane )
  194.                 panel = (PrefPaneID::ID)expand;
  195.         
  196.             // Find the row 
  197.             TableIndexT wideOpenRow = mTable->FindMessage( panel );
  198.             
  199.             // Now have to collapse the all the rows except appearence
  200.             // This is icky
  201.             mTable->CollapseRow( mTable->FindMessage ( PrefPaneID::eBrowser_Main  ) );
  202.             #ifdef MOZ_MAIL_NEWS
  203.             mTable->CollapseRow( mTable->FindMessage ( PrefPaneID::eMailNews_Main ) );
  204.             #endif // MOZ_MAIL_NEWS
  205.             #ifdef EDITOR
  206.             mTable->CollapseRow( mTable->FindMessage ( PrefPaneID::eEditor_Main ) );
  207.             #endif // EDITOR
  208.             #ifdef MOZ_OFFLINE
  209.             mTable->CollapseRow( mTable->FindMessage ( PrefPaneID::eOffline_Main ) );
  210.             #endif // MOZ_OFFLINE
  211.             mTable->CollapseRow( mTable->FindMessage ( PrefPaneID::eAdvanced_Main ) );    
  212.         
  213.             
  214.             mTable->UnselectAllCells();
  215.             STableCell initialCell( 1, 1 );
  216.             
  217.             if (PrefPaneID::eNoPaneSpecified != panel)
  218.             {    
  219.                 // Reveal the selection
  220.                 mTable->RevealRow( wideOpenRow );
  221.                 if ( pane == 0 )
  222.                     mTable->DeepExpandRow( wideOpenRow );
  223.                 
  224.                 initialCell.row =
  225.                     mTable->GetExposedIndex( wideOpenRow );
  226.                 
  227.             }
  228.             
  229.             mTable->SelectCell(initialCell);
  230.             // really should scroll selected category into view
  231.             LCommander::SetUpdateCommandStatus(true);
  232.             
  233.             CStr255 windowTitle;
  234.             GetUserWindowTitle(6, windowTitle);
  235.             mWindow->SetDescriptor(windowTitle);
  236.             
  237.             mWindow->Show();
  238.         }
  239.     }
  240. }
  241.  
  242. //-----------------------------------
  243. void CPrefsDialog::LoadICDependent()
  244. //-----------------------------------
  245. {
  246.     sThis->GetMediator(PrefPaneID::eBrowser_Main)->LoadPanes();            // home page
  247.         // we don't actually load the eMailNews_Identity manager because it will always
  248.         // alreay be loaded
  249. //    sThis->GetMediator(eMailNews_Identity)->LoadPanes();        // User Name
  250.                                                                 // User Email
  251.                                                                 // Organization
  252. #ifdef MOZ_MAIL_NEWS
  253.     sThis->GetMediator(PrefPaneID::eMailNews_MailServer)->LoadPanes();    // POP ID
  254.                                                                 // POP host
  255.                                                                 // SMTP host
  256.     sThis->GetMediator(PrefPaneID::eMailNews_NewsServer)->LoadPanes();    // News host
  257. #endif // MOZ_MAIL_NEWS
  258. } // CPrefsDialog::LoadICDependent
  259.  
  260.  
  261.  
  262. //-----------------------------------
  263. CPrefsMediator* CPrefsDialog::FindMediator(ResIDT paneID)
  264. //-----------------------------------
  265. {
  266.     LArrayIterator iterator(mPanels, LArrayIterator::from_Start);
  267.     CPrefsMediator* prefManager;
  268.     while (iterator.Next(&prefManager))
  269.     {
  270.         if ( prefManager->GetMainPaneID() == paneID )
  271.             return prefManager;    
  272.     }    
  273.     return nil;    
  274. } // CPrefsDialog::FindMediator
  275.  
  276. //-----------------------------------
  277. CPrefsMediator* CPrefsDialog::GetMediator(ResIDT inPaneID)
  278. //-----------------------------------
  279. {
  280.     if (inPaneID == PrefPaneID::eNoPaneSpecified)
  281.         return nil;
  282.     CPrefsMediator* result = FindMediator(inPaneID);
  283.     if (result)
  284.         return result;
  285.  
  286.     // The pane ID of the pane is equal to the class ID of the corresponding mediator
  287.     // Currently, we don't read in any resource data for a mediator, there are no resources,
  288.     // so the LStream* parameter is nil.  We just use the factory feature of the registrar.
  289.     // Later, we may decide to use UReanimator::ObjectsFromStream();
  290.     result = (CPrefsMediator*)URegistrar::CreateObject(inPaneID, nil);
  291.     if (!result)
  292.     {
  293.         // There's no special mediator with this class ID.  So give them a default mediator.
  294.         result = new CPrefsMediator(inPaneID);
  295.     }
  296.     mPanels.InsertItemsAt(1,LArray::index_Last, &result, sizeof( result ) );
  297.     AddListener(result);
  298.     return result;
  299. } // CPrefsDialog::GetMediator
  300.  
  301. //-----------------------------------
  302. void CPrefsDialog::FindCommandStatus(
  303.     CommandT    inCommand,
  304.     Boolean        &outEnabled,
  305.     Boolean&    /* outUsesMark */,
  306.     Char16&        /* outMark */,
  307.     Str255        /* outName */)
  308. //-----------------------------------
  309. {
  310.     // Don't enable any commands except cmd_About, and cmd_AboutPlugins
  311.     // which will keep the Apple menu enabled. This function purposely
  312.     // does not call the inherited FindCommandStatus, thereby suppressing
  313.     // commands that are handled by SuperCommanders. Only those
  314.     // commands enabled by SubCommanders will be active.
  315.     //
  316.     // This is usually what you want for a moveable modal dialog.
  317.     // Commands such as "New", "Open" and "Quit" that are handled
  318.     // by the Applcation are disabled, but items within the dialog
  319.     // can enable commands. For example, an EditField would enable
  320.     // items in the "Edit" menu.
  321.         
  322.     outEnabled = false;
  323.     if ((cmd_About == inCommand) || (cmd_AboutPlugins == inCommand))
  324.     {
  325.         outEnabled = true;
  326.     }
  327. }
  328.  
  329. //-----------------------------------
  330. Boolean CPrefsDialog::AllowTargetSwitch(LCommander *inNewTarget)
  331. //-----------------------------------
  332. {
  333.     CValidEditField    *target = dynamic_cast<CValidEditField*>(LCommander::GetTarget());
  334.     if (target)
  335.         return target->AllowTargetSwitch(inNewTarget);
  336.     return LCommander::AllowTargetSwitch(inNewTarget);
  337. }
  338.  
  339. //-----------------------------------
  340. void CPrefsDialog::ListenToMessage(
  341.                             MessageT        inMessage,
  342.                             void            */*ioParam*/)
  343. //-----------------------------------
  344. {
  345.     switch (inMessage)
  346.     {
  347.         case CMenuTable::msg_SelectionChanged:
  348.             MessageT    theViewID = mTable->GetSelectedMessage();
  349.             CPrefsMediator    *mediator = GetMediator(theViewID);
  350.             if (mediator)
  351.             {
  352.                 if (mCurrentMediator)
  353.                     mCurrentMediator->StepAside();
  354.                 mediator->StepForward(mWindow);
  355.                 mCurrentMediator = mediator;
  356.             }
  357.             break;
  358.         case CPrefsMediator::eCommitPrefs:
  359. #ifdef MOZ_MAIL_NEWS
  360.             CMailNewsMailServerMediator    *serverMediator = nil;
  361.             // check if the Mail server pane manager exist
  362.             // if it does get it
  363.             if ( FindMediator( PrefPaneID::eMailNews_MailServer ) )
  364.             {
  365.                 serverMediator
  366.                     = dynamic_cast<CMailNewsMailServerMediator*>(
  367.                         GetMediator(PrefPaneID::eMailNews_MailServer));
  368.             }
  369. #endif // MOZ_MAIL_NEWS
  370.             if (CPrefsMediator::CanSwitch())
  371.             {
  372. #ifdef MOZ_MAIL_NEWS // Is MOZ_MAIL_NEWS the correct symbol to use?
  373.                 Boolean forceQuit = false;
  374.                 Boolean allowCommit = true;
  375.                 // Add code here to check if we should quit or not...
  376.                 if (allowCommit)
  377.                 {
  378. #endif // MOZ_MAIL_NEWS
  379.                     BroadcastMessage(CPrefsMediator::eCommitPrefs, nil);
  380.                     Finished();
  381. #ifdef MOZ_MAIL_NEWS
  382.                     if (mNeedToCheckForVCard)
  383.                     {
  384.                         CheckForVCard();
  385.                     }
  386.                     if (forceQuit)
  387.                     {
  388.                         CFrontApp* app = dynamic_cast<CFrontApp*>(LCommander::GetTopCommander());
  389.                         if (app)
  390.                             app->SendAEQuit();
  391.                     }    
  392.                 }
  393.                 else
  394.                 {
  395.                     // we don't want to quit
  396.                     mCurrentMediator->StepAside();
  397.                     serverMediator->StepForward(mWindow);
  398.                 /* Don't reset the button back
  399.                     serverMediator->ResetServerButtons();
  400.                 */
  401.                     TableIndexT wideOpenRow = mTable->FindMessage(PrefPaneID::eMailNews_MailServer);
  402.                     mTable->RevealRow( wideOpenRow  );
  403.                     
  404.                     STableCell mailServerCell;
  405.                     mTable->IndexToCell( wideOpenRow, mailServerCell );
  406.                     mTable->SelectCell( mailServerCell );
  407.                 }
  408. #endif // MOZ_MAIL_NEWS
  409.             }
  410.             break;
  411.         case CPrefsMediator::eCancelPrefs:
  412.             BroadcastMessage(CPrefsMediator::eCancelPrefs, nil);
  413.             Finished();
  414.             break;
  415.         case eHelpButtonID:
  416.             if (mCurrentMediator)
  417.                 mCurrentMediator->ActivateHelp();
  418.             break;
  419.         default:
  420.             break;
  421.     }
  422. } // CPrefsDialog::ListenToMessage
  423.  
  424. //-----------------------------------
  425. void CPrefsDialog::Finished()
  426. //-----------------------------------
  427. {
  428.     mWindow->DoClose();
  429.     mWindow = nil;
  430.     sThis = nil;
  431.     if (MPreferenceBase::GetWriteOnDestroy()) // the controls wrote themselves
  432.         PREF_SavePrefFile();
  433.     delete this;
  434. }
  435.  
  436. //-----------------------------------
  437. void CPrefsDialog::RegisterViewClasses()
  438. //-----------------------------------
  439. {
  440.     // Mediator classes:
  441.     RegisterClass_(CAppearanceMainMediator);
  442.     RegisterClass_(CAppearanceFontsMediator);
  443.     RegisterClass_(CAppearanceColorsMediator);
  444.     RegisterClass_(CBrowserMainMediator);
  445.     RegisterClass_(CBrowserLanguagesMediator);
  446.     RegisterClass_(CBrowserApplicationsMediator);
  447.     RegisterClass_(CAdvancedCacheMediator);
  448.     RegisterClass_(CAdvancedProxiesMediator);
  449. #ifdef MOZ_MAIL_NEWS
  450.     RegisterClass_(CMailNewsIdentityMediator);
  451.     RegisterClass_(CMailNewsMainMediator);
  452.     RegisterClass_(CMailNewsMessagesMediator);
  453.     RegisterClass_(CMailNewsOutgoingMediator);
  454.     RegisterClass_(CMailNewsMailServerMediator);
  455.     RegisterClass_(CMailNewsNewsServerMediator);
  456.     RegisterClass_(CReceiptsMediator);
  457.     RegisterClass_(CMailNewsDirectoryMediator);
  458. #endif // MOZ_MAIL_NEWS
  459. #ifdef EDITOR
  460.     RegisterClass_(CEditorMainMediator);
  461. #endif
  462. #ifdef MOZ_OFFLINE
  463.     RegisterClass_(COfflineNewsMediator);
  464. #endif
  465. #ifdef MOZ_LDAP
  466.     // And a dialog class:
  467.     RegisterClass_(CLDAPServerPropDialog);
  468. #endif
  469. } // CPrefsDialog::RegisterViewClasses
  470.