Ticket #2210: RevertTimezone.diff

File RevertTimezone.diff, 12.8 KB (added by stpere, 16 years ago)

Add revert feature for the timezones in Time Preflet (with bugfix)

  • src/preferences/time/Time.cpp

     
    4646{
    4747    BAlert *alert = new BAlert("about",
    4848        "Time & Date, writen by:\n\n\tAndrew Edward McCall\n\tMike Berg\n\t"
    49         "Julun\n\nCopyright 2004-2007, Haiku.", "OK");
     49        "Julun\n\tPhilippe Saint-Pierre\n\nCopyright 2004-2008, Haiku.", "OK");
    5050    alert->Go();
    5151}
    5252
  • src/preferences/time/ZoneView.cpp

     
    11/*
    2  * Copyright 2004-2007, Haiku, Inc. All Rights Reserved.
     2 * Copyright 2004-2008, Haiku, Inc. All Rights Reserved.
    33 * Distributed under the terms of the MIT License.
    44 *
    55 * Authors:
    66 *      Mike Berg <mike@berg-net.us>
    77 *      Julun <host.haiku@gmx.de>
     8 *      Philippe Saint-Pierre <stpere@gmail.com>
    89 */
    910
    1011/*
     
    2122#include "ZoneView.h"
    2223#include "TimeMessages.h"
    2324#include "TZDisplay.h"
     25#include "TimeWindow.h"
    2426
    2527
    2628#include <Button.h>
     
    3739#include <StorageDefs.h>
    3840#include <String.h>
    3941#include <View.h>
     42#include <Window.h>
    4043
    4144
    4245#include <stdio.h>
     
    6770}
    6871
    6972
     73bool
     74TimeZoneView::CheckCanRevert()
     75{
     76    return fCurrentZone != fOldZone;
     77}
     78
     79
     80void
     81TimeZoneView::_Revert()
     82{
     83    BPath parent;
     84
     85    fCurrentZone = fOldZone;
     86    int32 czone;
     87
     88    if (strcmp(fCurrentZone.Leaf(), "Greenwich") == 0) {
     89        fRegionPopUp->FindItem("Others")->SetMarked(true);
     90        czone = FillCityList("Others");
     91    } else {
     92        fCurrentZone.GetParent(&parent);
     93        fRegionPopUp->FindItem(parent.Leaf())->SetMarked(true);
     94        czone = FillCityList(parent.Path());
     95    }
     96
     97    if (czone > -1) {
     98        fCityList->Select(czone);
     99        fCityList->ScrollToSelection();
     100        fCurrent->SetText(((TZoneItem *)fCityList->ItemAt(czone))->Text());
     101        SetPreview();
     102    }
     103    SetTimeZone();
     104}
     105
     106
    70107TimeZoneView::~TimeZoneView()
    71108{
    72109}
     
    87124
    88125        // update displays 
    89126        BPath parent;
    90         fCurrentZone.GetParent(&parent);
    91         int32 czone = FillCityList(parent.Path());
    92         if (czone > -1) {
    93             fCityList->Select(czone);
    94             fCurrent->SetText(((TZoneItem *)fCityList->ItemAt(czone))->Text());
     127        if (strcmp(fCurrentZone.Leaf(), "Greenwich") != 0) {
     128            fCurrentZone.GetParent(&parent);
     129            int32 czone = FillCityList(parent.Path());
     130            if (czone > -1) {
     131                fCityList->Select(czone);
     132                fCurrent->SetText(((TZoneItem *)fCityList->ItemAt(czone))->Text());
     133            }
     134        } else {
     135            int32 czone = FillCityList("Others");
     136            if (czone > -1) {
     137                fCityList->Select(czone);
     138                fCurrent->SetText(((TZoneItem *)fCityList->ItemAt(czone))->Text());
     139            }
    95140        }
    96141    }
    97142    fCityList->ScrollToSelection();
     
    120165            break;
    121166       
    122167        case H_SET_TIME_ZONE:
     168        {
    123169            SetTimeZone();
     170            BMessage msg(*message);
     171            msg.what = kRTCUpdate;
     172            Window()->PostMessage(&msg);
     173            ((TTimeWindow*)Window())->SetRevertStatus();
    124174            break;
    125        
     175        }
     176
     177        case kMsgRevert:
     178            _Revert();
     179            break;
     180
    126181        case H_CITY_CHANGED:
    127182            SetPreview();
    128183            break; 
     
    242297           
    243298            // skip Etc directory
    244299            if (itemtext.Compare("Etc", 3) == 0)
    245                 continue;
     300                continue;
    246301             
    247302            markit = itemtext.Compare(region.Leaf()) == 0;
    248303           
     
    263318            fRegionPopUp->AddItem(item);
    264319        }
    265320    }
     321    BMessage *msg = new BMessage(H_REGION_CHANGED);
     322    msg->AddString("region", "Others");
     323
     324    item = new BMenuItem("Others", msg);
     325    item->SetMarked(strcmp(fCurrentZone.Leaf(), "Greenwich") == 0);
     326    fRegionPopUp->AddItem(item);
    266327}
    267328
    268329
     
    277338        fCityList->MakeEmpty();
    278339    }
    279340
    280     // Enter time zones directory. Find subdir with name that matches string
    281     // stored in area. Enter subdirectory and count the items. For each item,
    282     // add a StringItem to fCityList Time zones directory
     341    BStringItem *city;
     342    int32 index = -1;
     343    if (strcmp(area, "Others") != 0) {
    283344
    284     BPath path;
    285     if (find_directory(B_BEOS_ETC_DIRECTORY, &path) != B_OK)
    286         return 0;
     345        // Enter time zones directory. Find subdir with name that matches string
     346        // stored in area. Enter subdirectory and count the items. For each item,
     347        // add a StringItem to fCityList Time zones directory
    287348
    288     path.Append("timezones");
     349        BPath path;
     350        if (find_directory(B_BEOS_ETC_DIRECTORY, &path) != B_OK)
     351            return 0;
    289352
    290     BPath Area(area);
    291     BDirectory zoneDir(path.Path());
    292     BDirectory cityDir;
    293     BStringItem *city;
    294     BString city_name;
    295     BEntry entry;
    296     int32 index = -1;
     353        path.Append("timezones");
     354
     355        BPath Area(area);
     356        BDirectory zoneDir(path.Path());
     357        BDirectory cityDir;
     358        BString city_name;
     359        BEntry entry;
     360
    297361   
    298     //locate subdirectory:
    299     if (zoneDir.Contains(Area.Leaf(), B_DIRECTORY_NODE)) {
    300         cityDir.SetTo(&zoneDir, Area.Leaf()); 
     362        //locate subdirectory:
     363        if (zoneDir.Contains(Area.Leaf(), B_DIRECTORY_NODE)) {
     364            cityDir.SetTo(&zoneDir, Area.Leaf()); 
    301365
    302         // There is a subdir with a name that matches 'area'. That's the one!!
    303         // Iterate over the items in the subdir, fill listview with TZoneItems
    304         while(cityDir.GetNextEntry(&entry) == B_NO_ERROR) {
    305             if (!entry.IsDirectory()) {
    306                 BPath zone(&entry);
    307                 city_name = zone.Leaf();
    308                 city_name.ReplaceAll("_IN", ", Indiana");
    309                 city_name.ReplaceAll("__Calif", ", Calif");
    310                 city_name.ReplaceAll("__", ", ");
    311                 city_name.ReplaceAll("_", " ");
    312                 city = new TZoneItem(city_name.String(), zone.Path());
    313                 fCityList->AddItem(city);
    314                 if (strcmp(fCurrentZone.Leaf(), zone.Leaf()) == 0)
    315                     index = fCityList->IndexOf(city);
     366            // There is a subdir with a name that matches 'area'. That's the one!!
     367            // Iterate over the items in the subdir, fill listview with TZoneItems
     368            while(cityDir.GetNextEntry(&entry) == B_NO_ERROR) {
     369                if (!entry.IsDirectory()) {
     370                    BPath zone(&entry);
     371                    city_name = zone.Leaf();
     372                    city_name.ReplaceAll("_IN", ", Indiana");
     373                    city_name.ReplaceAll("__Calif", ", Calif");
     374                    city_name.ReplaceAll("__", ", ");
     375                    city_name.ReplaceAll("_", " ");
     376                    city = new TZoneItem(city_name.String(), zone.Path());
     377                    fCityList->AddItem(city);
     378                    if (strcmp(fCurrentZone.Leaf(), zone.Leaf()) == 0)
     379                        index = fCityList->IndexOf(city);
     380                }
    316381            }
    317382        }
     383    } else {
     384        city = new TZoneItem("Greenwich", "/boot/beos/etc/timezones/Greenwich");
     385        fCityList->AddItem(city);
     386        if (strcmp(fCurrentZone.Leaf(), "Greenwich") == 0) {
     387            index = fCityList->IndexOf(city);
     388        }
    318389    }
    319390    return index;
    320391}
     
    363434            tzLink.SetTo("/boot/beos/etc/timezones/Pacific/fiji");
    364435            // do something like set to a default GMT???
    365436        }
     437        else if (!tzLink.Exists()) { // link doesn't exists
     438            tzLink.SetTo("/boot/beos/etc/timezones/Greenwich");
     439        }
    366440    } else {
    367         // set tzlink to a default
     441        // directory doesn't exist
    368442        tzLink.SetTo("/boot/beos/etc/timezones/EST");
    369443    }
    370444#endif
    371445    // we need something in the current zone
    372446    fCurrentZone.SetTo(&tzLink);
     447    fOldZone.SetTo(&tzLink);
    373448}
    374449
    375450
     
    449524
    450525    // update environment
    451526    SetTimeZone(target.Path());
    452 
     527   
    453528    // update display
    454529    time_t current = time(0);
    455530    struct tm *ltime = localtime(&current);
  • src/preferences/time/ZoneView.h

     
    2828       
    2929        virtual void    AttachedToWindow();
    3030        virtual void    MessageReceived(BMessage *message);
     31        bool            CheckCanRevert();
    3132
    3233    private:
    3334        void            UpdateDateTime(BMessage *message);
     
    3940        void            InitView();
    4041        void            ReadTimeZoneLink();
    4142        void            BuildRegionMenu();
     43        void            _Revert();
    4244
    4345        // returns index of current zone
    4446        int32           FillCityList(const char *area);
     
    5355        int32           fHour;
    5456        int32           fMinute;
    5557        BPath           fCurrentZone;
     58        BPath               fOldZone;
    5659        bool            fInitialized;
    5760};
    5861
  • src/preferences/time/TimeWindow.cpp

     
    2020#include <Message.h>
    2121#include <Screen.h>
    2222#include <TabView.h>
     23#include <Button.h>
    2324
    2425
    2526TTimeWindow::TTimeWindow(BRect rect)
     
    3738}
    3839
    3940
     41void
     42TTimeWindow::SetRevertStatus()
     43{
     44    fRevertButton->SetEnabled(fDateTimeView->CheckCanRevert()
     45                                        || fTimeZoneView->CheckCanRevert());
     46}
     47
     48
    4049void
    4150TTimeWindow::MessageReceived(BMessage *message)
    4251{
    4352    switch(message->what) {
    4453        case H_USER_CHANGE:
    4554            fBaseView->ChangeTime(message);
    46             fDateTimeView->CheckCanRevert();
     55            SetRevertStatus();
    4756            break;
    4857       
    4958        case B_ABOUT_REQUESTED:
    5059            be_app->PostMessage(B_ABOUT_REQUESTED);
    5160            break;
    5261
     62        case kMsgRevert:
     63            fDateTimeView->MessageReceived(message);
     64            fTimeZoneView->MessageReceived(message);
     65            fRevertButton->SetEnabled(false);
     66            break;
     67
     68        case kRTCUpdate:
     69            fDateTimeView->MessageReceived(message);
     70            SetRevertStatus();
     71            break;
     72
    5373        default:
    5474            BWindow::MessageReceived(message);
    5575            break;
     
    101121
    102122    fBaseView->AddChild(tabView);
    103123    tabView->ResizeBy(0.0, tabView->TabHeight());
     124
     125    BRect rect = Bounds();
     126
     127    rect.left = 10;
     128    rect.top = rect.bottom - 10;
     129
     130    fRevertButton = new BButton(rect, "revert", "Revert",
     131        new BMessage(kMsgRevert), B_FOLLOW_LEFT | B_FOLLOW_BOTTOM, B_WILL_DRAW);
     132   
     133    fRevertButton->ResizeToPreferred();
     134    fRevertButton->SetEnabled(false);
     135    float buttonHeight = fRevertButton->Bounds().Height();
     136    fRevertButton->MoveBy(0, -buttonHeight);
     137    fBaseView->AddChild(fRevertButton);
     138    fRevertButton->SetTarget(this);
     139
    104140    fBaseView->ResizeTo(tabView->Bounds().Width() + 10.0,
    105         tabView->Bounds().Height() + 10.0);
     141        tabView->Bounds().Height() + buttonHeight + 30.0);
    106142
    107143    ResizeTo(fBaseView->Bounds().Width(), fBaseView->Bounds().Height());
    108144}
  • src/preferences/time/DateTimeView.cpp

     
    1515#include "DateTimeEdit.h"
    1616#include "TimeMessages.h"
    1717#include "DateTime.h"
     18#include "TimeWindow.h"
    1819
    1920
    20 #include <Button.h>
    2121#include <CheckBox.h>
    2222#include <Entry.h>
    2323#include <File.h>
     
    6969    if (!fInitialized) {
    7070        fInitialized = true;
    7171
    72         fGmtTime->SetTarget(this);
    73         fLocalTime->SetTarget(this);
    7472        fCalendarView->SetTarget(this);
    75         fRevertButton->SetTarget(this);
    7673    }
    7774}
    7875
     
    131128        case kRTCUpdate:
    132129            fUseGmtTime = fGmtTime->Value() == B_CONTROL_ON;
    133130            _UpdateGmtSettings();
    134             CheckCanRevert();
    135131            break;
    136132
    137133        case kMsgRevert:
    138134            _Revert();
    139             fRevertButton->SetEnabled(false);
    140135            break;
    141136   
    142137        default:
     
    146141}
    147142
    148143
    149 void
     144bool
    150145DateTimeView::CheckCanRevert()
    151146{
    152147    // check GMT vs Local setting
     
    157152    time_t changedNow;
    158153    time(&changedNow);
    159154
    160     enable = enable || (changedNow != unchangedNow);
    161 
    162     fRevertButton->SetEnabled(enable);
     155    return enable || (changedNow != unchangedNow);
    163156}
    164157
    165158
     
    266259
    267260    fOldUseGmtTime = fUseGmtTime;
    268261
    269     BRect rect = Bounds();
    270 
    271     rect.left = 10;
    272     rect.top = rect.bottom - 10;
    273 
    274     fRevertButton = new BButton(rect, "revert", "Revert",
    275         new BMessage(kMsgRevert), B_FOLLOW_LEFT | B_FOLLOW_BOTTOM, B_WILL_DRAW);
    276    
    277     fRevertButton->ResizeToPreferred();
    278     fRevertButton->SetEnabled(false);
    279     float buttonHeight = fRevertButton->Bounds().Height();
    280     fRevertButton->MoveBy(0, -buttonHeight);
    281     AddChild(fRevertButton);
    282 
    283262    ResizeTo(fClock->Frame().right + 10.0, fGmtTime->Frame().bottom + 10.0);
    284263}
    285264
  • src/preferences/time/TimeWindow.h

     
    2727
    2828        virtual bool    QuitRequested();
    2929        virtual void    MessageReceived(BMessage *message);
     30        void                SetRevertStatus();
    3031
    3132    private:
    3233        void            _InitWindow();
     
    3637        TTimeBaseView   *fBaseView;
    3738        DateTimeView    *fDateTimeView;
    3839        TimeZoneView    *fTimeZoneView;
     40        BButton         *fRevertButton;
    3941};
    4042
    4143#endif  // TIME_WINDOW_H
  • src/preferences/time/DateTimeView.h

     
    2020class BCalendarView;
    2121class BRadioButton;
    2222class TAnalogClock;
    23 class BButton;
    2423
    2524
    2625class DateTimeView : public BView {
     
    3231        virtual void    Draw(BRect updaterect);
    3332        virtual void    MessageReceived(BMessage *message);
    3433
    35                 void    CheckCanRevert();
     34        bool            CheckCanRevert();
     35        bool            GetUseGmtTime();
    3636
    3737    private:
    3838        void            _InitView();
     
    5151        BCalendarView   *fCalendarView;
    5252        TAnalogClock    *fClock;
    5353
    54         BButton         *fRevertButton;
    55 
    56         bool            fUseGmtTime;
     54        bool            fUseGmtTime;
    5755        bool            fOldUseGmtTime;
    5856        bool            fInitialized;
    5957