ktimezonewidget.cpp
00001 /* 00002 Copyright (C) 2005, S.R.Haque <srhaque@iee.org>. 00003 This file is part of the KDE project 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License version 2, as published by the Free Software Foundation. 00008 00009 This library is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00012 Library General Public License for more details. 00013 00014 You should have received a copy of the GNU Library General Public License 00015 along with this library; see the file COPYING.LIB. If not, write to 00016 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00017 Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include <kdialog.h> 00021 #include <kdebug.h> 00022 #include <tdefile.h> 00023 #include <tdelistview.h> 00024 #include <tdelocale.h> 00025 #include <kstandarddirs.h> 00026 #include <ktimezones.h> 00027 #include <ktimezonewidget.h> 00028 #include <tqpixmap.h> 00029 #include <time.h> 00030 00031 #define COLUMN_CITY 0 00032 #define COLUMN_REGION 1 00033 #define COLUMN_COMMENT 2 00034 #define COLUMN_ZONE 3 00035 00036 KTimezoneWidget::KTimezoneWidget(TQWidget *parent, const char *name, KTimezones *db) : 00037 TDEListView(parent, name), 00038 d(0) 00039 { 00040 // If the user did not provide a timezone database, we'll use the system default. 00041 bool userDb = (db != 0); 00042 if (!userDb) 00043 db = new KTimezones(); 00044 00045 addColumn(i18n("Area")); 00046 addColumn(i18n("Region")); 00047 addColumn(i18n("Comment")); 00048 00049 const KTimezones::ZoneMap zones = db->allZones(); 00050 for (KTimezones::ZoneMap::ConstIterator it = zones.begin(); it != zones.end(); ++it) 00051 { 00052 const KTimezone *zone = it.data(); 00053 TQString tzName = zone->name(); 00054 TQString comment = zone->comment(); 00055 if (!comment.isEmpty()) 00056 comment = i18n(comment.utf8()); 00057 00058 // Convert: 00059 // 00060 // "Europe/London", "GB" -> "London", "Europe/GB". 00061 // "UTC", "" -> "UTC", "". 00062 TQStringList continentCity = TQStringList::split("/", displayName(zone)); 00063 TQListViewItem *listItem = new TQListViewItem(this, continentCity[continentCity.count() - 1]); 00064 continentCity[continentCity.count() - 1] = zone->countryCode(); 00065 listItem->setText(COLUMN_REGION, continentCity.join("/")); 00066 listItem->setText(COLUMN_COMMENT, comment); 00067 listItem->setText(COLUMN_ZONE, tzName); /* store complete path in ListView */ 00068 00069 // Locate the flag from /l10n/%1/flag.png. 00070 TQString flag = locate("locale", TQString("l10n/%1/flag.png").arg(zone->countryCode().lower())); 00071 if (TQFile::exists(flag)) 00072 listItem->setPixmap(COLUMN_REGION, TQPixmap(flag)); 00073 } 00074 00075 if (!userDb) 00076 delete db; 00077 00078 } 00079 00080 KTimezoneWidget::~KTimezoneWidget() 00081 { 00082 // FIXME when needed: 00083 // delete d; 00084 } 00085 00086 TQString KTimezoneWidget::displayName(const KTimezone *zone) 00087 { 00088 return i18n(zone->name().utf8()).replace("_", " "); 00089 } 00090 00091 TQStringList KTimezoneWidget::selection() const 00092 { 00093 TQStringList selection; 00094 00095 // Loop through all entries. 00096 TQListViewItem *listItem = firstChild(); 00097 while (listItem) 00098 { 00099 if (listItem->isSelected()) 00100 { 00101 selection.append(listItem->text(COLUMN_ZONE)); 00102 } 00103 listItem = listItem->nextSibling(); 00104 } 00105 return selection; 00106 } 00107 00108 void KTimezoneWidget::setSelected(const TQString &zone, bool selected) 00109 { 00110 bool found = false; 00111 00112 // Loop through all entries. 00113 TQListViewItem *listItem = firstChild(); 00114 while (listItem) 00115 { 00116 if (listItem->text(COLUMN_ZONE) == zone) 00117 { 00118 TDEListView::setSelected(listItem, selected); 00119 00120 // Ensure the selected item is visible as appropriate. 00121 listItem = selectedItem(); 00122 if (listItem) 00123 ensureItemVisible(listItem); 00124 found = true; 00125 break; 00126 } 00127 listItem = listItem->nextSibling(); 00128 } 00129 if (!found) 00130 kdDebug() << "No such zone: " << zone << endl; 00131 } 00132 00133 #include "ktimezonewidget.moc"