Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

PageProfileProfile.cpp

Go to the documentation of this file.
00001 //
00002 // PageProfileProfile.cpp
00003 //
00004 // Copyright (c) Shareaza Development Team, 2002-2005.
00005 // This file is part of SHAREAZA (www.shareaza.com)
00006 //
00007 // Shareaza is free software; you can redistribute it
00008 // and/or modify it under the terms of the GNU General Public License
00009 // as published by the Free Software Foundation; either version 2 of
00010 // the License, or (at your option) any later version.
00011 //
00012 // Shareaza is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 //
00017 // You should have received a copy of the GNU General Public License
00018 // along with Shareaza; if not, write to the Free Software
00019 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00020 //
00021 
00022 #include "StdAfx.h"
00023 #include "Shareaza.h"
00024 #include "Settings.h"
00025 #include "GProfile.h"
00026 #include "XML.h"
00027 #include "PageProfileProfile.h"
00028 #include "WorldGPS.h"
00029 
00030 #ifdef _DEBUG
00031 #define new DEBUG_NEW
00032 #undef THIS_FILE
00033 static char THIS_FILE[] = __FILE__;
00034 #endif
00035 
00036 IMPLEMENT_DYNCREATE(CProfileProfilePage, CSettingsPage)
00037 
00038 BEGIN_MESSAGE_MAP(CProfileProfilePage, CSettingsPage)
00039         //{{AFX_MSG_MAP(CProfileProfilePage)
00040         ON_CBN_CLOSEUP(IDC_LOC_COUNTRY, OnCloseUpCountry)
00041         ON_CBN_CLOSEUP(IDC_LOC_CITY, OnCloseUpCity)
00042         ON_LBN_SELCHANGE(IDC_INTEREST_LIST, OnSelChangeInterestList)
00043         ON_CBN_SELCHANGE(IDC_INTEREST_ALL, OnSelChangeInterestAll)
00044         ON_CBN_EDITCHANGE(IDC_INTEREST_ALL, OnEditChangeInterestAll)
00045         ON_BN_CLICKED(IDC_INTEREST_ADD, OnInterestAdd)
00046         ON_BN_CLICKED(IDC_INTEREST_REMOVE, OnInterestRemove)
00047         //}}AFX_MSG_MAP
00048 END_MESSAGE_MAP()
00049 
00050 
00052 // CProfileProfilePage property page
00053 
00054 CProfileProfilePage::CProfileProfilePage() : CSettingsPage( CProfileProfilePage::IDD )
00055 {
00056         //{{AFX_DATA_INIT(CProfileProfilePage)
00057         m_sLocCity = _T("");
00058         m_sLocCountry = _T("");
00059         m_sLocLatitude = _T("");
00060         m_sLocLongitude = _T("");
00061         //}}AFX_DATA_INIT
00062         m_pWorld = NULL;
00063 }
00064 
00065 CProfileProfilePage::~CProfileProfilePage()
00066 {
00067         if ( m_pWorld ) delete m_pWorld;
00068 }
00069 
00070 void CProfileProfilePage::DoDataExchange(CDataExchange* pDX)
00071 {
00072         CSettingsPage::DoDataExchange(pDX);
00073         //{{AFX_DATA_MAP(CProfileProfilePage)
00074         DDX_Control(pDX, IDC_INTEREST_REMOVE, m_wndInterestRemove);
00075         DDX_Control(pDX, IDC_INTEREST_ADD, m_wndInterestAdd);
00076         DDX_Control(pDX, IDC_INTEREST_ALL, m_wndInterestAll);
00077         DDX_Control(pDX, IDC_INTEREST_LIST, m_wndInterestList);
00078         DDX_Control(pDX, IDC_LOC_CITY, m_wndCity);
00079         DDX_Control(pDX, IDC_LOC_COUNTRY, m_wndCountry);
00080         DDX_CBString(pDX, IDC_LOC_CITY, m_sLocCity);
00081         DDX_CBString(pDX, IDC_LOC_COUNTRY, m_sLocCountry);
00082         DDX_Text(pDX, IDC_LOC_LAT, m_sLocLatitude);
00083         DDX_Text(pDX, IDC_LOC_LONG, m_sLocLongitude);
00084         //}}AFX_DATA_MAP
00085 }
00086 
00088 // CProfileProfilePage message handlers
00089 
00090 BOOL CProfileProfilePage::OnInitDialog()
00091 {
00092         CSettingsPage::OnInitDialog();
00093 
00094         m_pWorld = new CWorldGPS();
00095         m_pWorld->Load();
00096 
00097         CWorldCountry* pCountry = m_pWorld->m_pCountry;
00098 
00099         for ( int nCountry = m_pWorld->m_nCountry ; nCountry ; nCountry--, pCountry++ )
00100         {
00101                 m_wndCountry.SetItemData( m_wndCountry.AddString( pCountry->m_sName ), (LPARAM)pCountry );
00102         }
00103 
00104         if ( CXMLElement* pLocation = MyProfile.GetXML( _T("location") ) )
00105         {
00106                 if ( CXMLElement* pPolitical = pLocation->GetElementByName( _T("political") ) )
00107                 {
00108                         m_sLocCountry   = pPolitical->GetAttributeValue( _T("country") );
00109                         m_sLocCity              = pPolitical->GetAttributeValue( _T("city") ) + _T(", ")
00110                                                         + pPolitical->GetAttributeValue( _T("state") );
00111                 }
00112 
00113                 if ( CXMLElement* pCoordinates = pLocation->GetElementByName( _T("coordinates") ) )
00114                 {
00115                         CString str = pCoordinates->GetAttributeValue( _T("latitude") );
00116                         float nValue;
00117 
00118                         if ( _stscanf( str, _T("%f"), &nValue ) == 1 )
00119                         {
00120                                 m_sLocLatitude.Format( nValue >= 0 ?
00121                                         _T("%.2f° N") : _T("%.2f° S"), double( fabs( nValue ) ) );
00122                         }
00123 
00124                         str = pCoordinates->GetAttributeValue( _T("longitude") );
00125 
00126                         if ( _stscanf( str, _T("%f"), &nValue ) == 1 )
00127                         {
00128                                 m_sLocLongitude.Format( nValue >= 0 ? _T("%.1f° E") : _T("%.1f° W"),
00129                                         double( fabs( nValue ) ) );
00130                         }
00131                 }
00132         }
00133 
00134         if ( CXMLElement* pInterests = MyProfile.GetXML( _T("interests") ) )
00135         {
00136                 for ( POSITION pos = pInterests->GetElementIterator() ; pos ; )
00137                 {
00138                         CXMLElement* pInterest = pInterests->GetNextElement( pos );
00139 
00140                         if ( pInterest->IsNamed( _T("interest") ) )
00141                         {
00142                                 m_wndInterestList.AddString( pInterest->GetValue() );
00143                         }
00144                 }
00145         }
00146 
00147         UpdateData( FALSE );
00148 
00149         m_wndInterestAdd.EnableWindow( FALSE );
00150         m_wndInterestRemove.EnableWindow( FALSE );
00151 
00152         OnCloseUpCountry();
00153 
00154         return TRUE;
00155 }
00156 
00157 void CProfileProfilePage::OnCloseUpCountry()
00158 {
00159         CWaitCursor pCursor;
00160 
00161         int nSel = m_wndCountry.GetCurSel();
00162         if ( nSel < 0 ) return;
00163         CWorldCountry* pCountry = (CWorldCountry*)m_wndCountry.GetItemData( nSel );
00164         if ( ! pCountry ) return;
00165 
00166         if ( m_wndCity.GetCount() ) m_wndCity.ResetContent();
00167 
00168         CWorldCity* pCity = pCountry->m_pCity;
00169         CString strCity;
00170 
00171         for ( int nCity = pCountry->m_nCity ; nCity ; nCity--, pCity++ )
00172         {
00173                 if ( pCity->m_sName.GetLength() )
00174                 {
00175                         if ( pCity->m_sState.GetLength() )
00176                                 strCity = pCity->m_sName + _T(", ") + pCity->m_sState;
00177                         else
00178                                 strCity = pCity->m_sName;
00179                 }
00180                 else if ( pCity->m_sState.GetLength() )
00181                 {
00182                         strCity = pCity->m_sState;
00183                 }
00184                 else
00185                 {
00186                         continue;
00187                 }
00188                 m_wndCity.SetItemData( m_wndCity.AddString( strCity ), (LPARAM)pCity );
00189         }
00190 }
00191 
00192 void CProfileProfilePage::OnCloseUpCity()
00193 {
00194         int nSel = m_wndCity.GetCurSel();
00195         if ( nSel < 0 ) return;
00196         CWorldCity* pCity = (CWorldCity*)m_wndCity.GetItemData( nSel );
00197         if ( ! pCity ) return;
00198 
00199         m_sLocLatitude.Format( pCity->m_nLatitude >= 0 ? _T("%.2f° N") : _T("%.2f° S"),
00200                 double( fabs( pCity->m_nLatitude ) ) );
00201 
00202         m_sLocLongitude.Format( pCity->m_nLongitude >= 0 ? _T("%.1f° E") : _T("%.1f° W"),
00203                 double( fabs( pCity->m_nLongitude ) ) );
00204 
00205         GetDlgItem( IDC_LOC_LAT )->SetWindowText( m_sLocLatitude );
00206         GetDlgItem( IDC_LOC_LONG )->SetWindowText( m_sLocLongitude );
00207 }
00208 
00209 void CProfileProfilePage::OnSelChangeInterestList()
00210 {
00211         m_wndInterestRemove.EnableWindow( m_wndInterestList.GetCurSel() >= 0 );
00212 }
00213 
00214 void CProfileProfilePage::OnSelChangeInterestAll()
00215 {
00216         m_wndInterestAdd.EnableWindow( m_wndInterestAll.GetCurSel() >= 0 || m_wndInterestAll.GetWindowTextLength() > 0 );
00217 }
00218 
00219 void CProfileProfilePage::OnEditChangeInterestAll()
00220 {
00221         m_wndInterestAdd.EnableWindow( m_wndInterestAll.GetCurSel() >= 0 || m_wndInterestAll.GetWindowTextLength() > 0 );
00222 }
00223 
00224 void CProfileProfilePage::OnInterestAdd()
00225 {
00226         CString str;
00227         m_wndInterestAll.GetWindowText( str );
00228         m_wndInterestList.AddString( str );
00229 }
00230 
00231 void CProfileProfilePage::OnInterestRemove()
00232 {
00233         int nItem = m_wndInterestList.GetCurSel();
00234         if ( nItem >= 0 ) m_wndInterestList.DeleteString( nItem );
00235         m_wndInterestRemove.EnableWindow( FALSE );
00236 }
00237 
00238 void CProfileProfilePage::OnOK()
00239 {
00240         UpdateData();
00241 
00242         if ( CXMLElement* pLocation = MyProfile.GetXML( _T("location"), TRUE ) )
00243         {
00244                 if ( CXMLElement* pPolitical = pLocation->GetElementByName( _T("political"), TRUE ) )
00245                 {
00246                         if ( m_sLocCountry.GetLength() )
00247                         {
00248                                 pPolitical->AddAttribute( _T("country"), m_sLocCountry );
00249                         }
00250                         else
00251                         {
00252                                 if ( CXMLAttribute* pAttr = pPolitical->GetAttribute( _T("country") ) )
00253                                         pAttr->Delete();
00254                         }
00255 
00256                         int nPos = m_sLocCity.Find( _T(", ") );
00257 
00258                         if ( nPos >= 0 )
00259                         {
00260                                 pPolitical->AddAttribute( _T("city"), m_sLocCity.Left( nPos ) );
00261                                 pPolitical->AddAttribute( _T("state"), m_sLocCity.Mid( nPos + 2 ) );
00262                         }
00263                         else if ( m_sLocCity.GetLength() )
00264                         {
00265                                 pPolitical->AddAttribute( _T("city"), m_sLocCity );
00266                                 if ( CXMLAttribute* pAttr = pPolitical->GetAttribute( _T("state") ) )
00267                                         pAttr->Delete();
00268                         }
00269                         else
00270                         {
00271                                 if ( CXMLAttribute* pAttr = pPolitical->GetAttribute( _T("city") ) )
00272                                         pAttr->Delete();
00273                                 if ( CXMLAttribute* pAttr = pPolitical->GetAttribute( _T("state") ) )
00274                                         pAttr->Delete();
00275                         }
00276 
00277                         if ( pPolitical->GetElementCount() == 0 && pPolitical->GetAttributeCount() == 0 )
00278                                 pPolitical->Delete();
00279                 }
00280 
00281                 if ( CXMLElement* pCoordinates = pLocation->GetElementByName( _T("coordinates"), TRUE ) )
00282                 {
00283                         CString strValue;
00284                         float nValue = 0;
00285 
00286                         if ( _stscanf( m_sLocLatitude, _T("%f"), &nValue ) == 1 )
00287                         {
00288                                 if ( m_sLocLatitude.Find( 'S' ) >= 0 ) nValue *= -1;
00289                                 strValue.Format( _T("%f"), double( nValue ) );
00290                                 pCoordinates->AddAttribute( _T("latitude"), strValue );
00291                         }
00292 
00293                         if ( _stscanf( m_sLocLongitude, _T("%f"), &nValue ) == 1 )
00294                         {
00295                                 if ( m_sLocLongitude.Find( 'W' ) >= 0 ) nValue *= -1;
00296                                 strValue.Format( _T("%f"), double( nValue ) );
00297                                 pCoordinates->AddAttribute( _T("longitude"), strValue );
00298                         }
00299 
00300                         if ( nValue == 0 ) pCoordinates->Delete();
00301                 }
00302 
00303                 if ( pLocation->GetElementCount() == 0 ) pLocation->Delete();
00304         }
00305 
00306         if ( CXMLElement* pInterests = MyProfile.GetXML( _T("interests"), TRUE ) )
00307         {
00308                 pInterests->DeleteAllElements();
00309 
00310                 for ( int nItem = 0 ; nItem < m_wndInterestList.GetCount() ; nItem++ )
00311                 {
00312                         CString str;
00313                         m_wndInterestList.GetText( nItem, str );
00314                         CXMLElement* pInterest = pInterests->AddElement( _T("interest") );
00315                         pInterest->SetValue( str );
00316                 }
00317 
00318                 if ( pInterests->GetElementCount() == 0 ) pInterests->Delete();
00319         }
00320 
00321         CSettingsPage::OnOK();
00322 }
00323 

Generated on Thu Dec 15 10:39:45 2005 for Shareaza 2.2.1.0 by  doxygen 1.4.2