aboutsummaryrefslogtreecommitdiff
path: root/src/ClockTool.cc
diff options
context:
space:
mode:
authorMathias Gumz <akira at fluxbox dot org>2010-08-24 15:30:24 (GMT)
committerMathias Gumz <akira at fluxbox dot org>2010-08-24 15:30:24 (GMT)
commitb8987fc6238a747285b3aeac6f3b206a74e0d36a (patch)
treef8826582a15deec69942310e4405aac3b412b4c0 /src/ClockTool.cc
parent874eb67297cd63999730d1e779ff00581dcbb89d (diff)
downloadfluxbox-b8987fc6238a747285b3aeac6f3b206a74e0d36a.zip
fluxbox-b8987fc6238a747285b3aeac6f3b206a74e0d36a.tar.bz2
code simplification / deduplication
* parse the string only once for a bunch of chars to check instead of starting over again and again from the beginning, created a helper function to do this (FbTk::StringUtil::findCharFromAlphabetAfterTrigger) * put same code into a function (setClockModeLabel()) * use much simpler code to switch between 12h and 24h mode and replace the fmt-switches
Diffstat (limited to 'src/ClockTool.cc')
-rw-r--r--src/ClockTool.cc88
1 files changed, 34 insertions, 54 deletions
diff --git a/src/ClockTool.cc b/src/ClockTool.cc
index 91fea93..c2d029f 100644
--- a/src/ClockTool.cc
+++ b/src/ClockTool.cc
@@ -50,6 +50,11 @@
50 50
51namespace { 51namespace {
52 52
53static const char SWITCHES_SECONDS[] = "crsSTX+";
54static const char SWITCHES_12_24H[] = "lIrkHT";
55static const char SWITCHES_24_12H[] = "kHTlIr";
56static const char SWITCH_AM_PM[] = "pP";
57
53/** 58/**
54 * return true if clock shows seconds. If clock doesn't show seconds then 59 * return true if clock shows seconds. If clock doesn't show seconds then
55 * there is no need to wake up every second to redraw the clock. 60 * there is no need to wake up every second to redraw the clock.
@@ -57,15 +62,11 @@ namespace {
57 62
58int showSeconds(const std::string& fmt_string) { 63int showSeconds(const std::string& fmt_string) {
59 64
60 return fmt_string.find("%c") != std::string::npos 65 return FbTk::StringUtil::findCharFromAlphabetAfterTrigger(
61 || fmt_string.find("%r") != std::string::npos 66 fmt_string, '%', SWITCHES_SECONDS, sizeof(SWITCHES_SECONDS), 0) != std::string::npos;
62 || fmt_string.find("%s") != std::string::npos
63 || fmt_string.find("%S") != std::string::npos
64 || fmt_string.find("%T") != std::string::npos
65 || fmt_string.find("%X") != std::string::npos
66 || fmt_string.find("%+") != std::string::npos;
67} 67}
68 68
69
69timeval calcNextTimeout(const std::string& fmt_string) { 70timeval calcNextTimeout(const std::string& fmt_string) {
70 timeval now; 71 timeval now;
71 timeval next; 72 timeval next;
@@ -93,69 +94,48 @@ class ClockMenuItem: public FbTk::MenuItem {
93public: 94public:
94 explicit ClockMenuItem(ClockTool &tool): 95 explicit ClockMenuItem(ClockTool &tool):
95 FbTk::MenuItem(""), m_tool(tool) { 96 FbTk::MenuItem(""), m_tool(tool) {
96 // determine 12/24 hour format 97
97 _FB_USES_NLS; 98 setClockModeLabel();
98 if (m_tool.timeFormat().find("%k") != std::string::npos ||
99 m_tool.timeFormat().find("%H") != std::string::npos ||
100 m_tool.timeFormat().find("%T") != std::string::npos)
101 setLabel( _FB_XTEXT(Toolbar, Clock24, "Clock: 24h", "set Clockmode to 24h") );
102 else
103 setLabel( _FB_XTEXT(Toolbar, Clock12, "Clock: 12h", "set Clockmode to 12h") );
104 setCloseOnClick(false); 99 setCloseOnClick(false);
105 } 100 }
106 101
107 void click(int button, int time, unsigned int mods) { 102 void click(int button, int time, unsigned int mods) {
108 std::string newformat = m_tool.timeFormat();
109 size_t pos = newformat.find("%k");
110 std::string newstr;
111 bool clock24hour = true;
112
113 _FB_USES_NLS;
114 103
115 if (pos != std::string::npos) 104 // does the current format string contain something with 24/12h?
116 newstr = "%l"; 105 size_t found;
117 else if ((pos = newformat.find("%H")) != std::string::npos) 106 size_t pos = FbTk::StringUtil::findCharFromAlphabetAfterTrigger(
118 newstr = "%I"; 107 m_tool.timeFormat(), '%', SWITCHES_24_12H, sizeof(SWITCHES_24_12H), &found);
119 else if ((pos = newformat.find("%T")) != std::string::npos)
120 newstr = "%r";
121
122 // 12 hour
123 if (newstr.empty()) {
124 clock24hour = false;
125 if ((pos = newformat.find("%l")) != std::string::npos)
126 newstr = "%k";
127 else if ((pos = newformat.find("%I")) != std::string::npos)
128 newstr = "%H";
129 else if ((pos = newformat.find("%r")) != std::string::npos)
130 newstr = "%T";
131
132 }
133 108
134 if (!newstr.empty()) { 109 if (pos != std::string::npos) { // if so, exchange it with 12/24h
110 std::string newformat = m_tool.timeFormat();
111 newformat[pos+1] = SWITCHES_12_24H[found];
135 112
136 newformat.replace(pos, 2, newstr); 113 if (found < 3) { // 24h? erase %P/%p (AM|PM / am|pm)
137 if (!clock24hour) { // erase %P/%p (AM|PM / am|pm) 114 pos = FbTk::StringUtil::findCharFromAlphabetAfterTrigger(
138 pos = newformat.find("%p"); 115 newformat, '%', SWITCH_AM_PM, sizeof(SWITCH_AM_PM), 0);
139 if (pos != std::string::npos) 116 if (pos != std::string::npos) {
140 newformat.erase(pos, 2);
141 else if ((pos = newformat.find("%P")) != std::string::npos)
142 newformat.erase(pos, 2); 117 newformat.erase(pos, 2);
118 }
143 } 119 }
144 120
145
146 m_tool.setTimeFormat(newformat); 121 m_tool.setTimeFormat(newformat);
147 122 setClockModeLabel();
148 if (m_tool.timeFormat().find("%k") != std::string::npos ||
149 m_tool.timeFormat().find("%H") != std::string::npos ||
150 m_tool.timeFormat().find("%T") != std::string::npos)
151 setLabel( _FB_XTEXT(Toolbar, Clock24, "Clock: 24h", "set Clockmode to 24h") );
152 else
153 setLabel( _FB_XTEXT(Toolbar, Clock12, "Clock: 12h", "set Clockmode to 12h") );
154 123
155 } // else some other strange format...so we don't do anything 124 } // else some other strange format...so we don't do anything
156 FbTk::MenuItem::click(button, time, mods); 125 FbTk::MenuItem::click(button, time, mods);
157 } 126 }
158private: 127private:
128
129 void setClockModeLabel() {
130 _FB_USES_NLS;
131 if (FbTk::StringUtil::findCharFromAlphabetAfterTrigger(
132 m_tool.timeFormat(), '%', SWITCHES_24_12H, 3, 0) != std::string::npos) {
133 setLabel( _FB_XTEXT(Toolbar, Clock24, "Clock: 24h", "set Clockmode to 24h") );
134 } else {
135 setLabel( _FB_XTEXT(Toolbar, Clock12, "Clock: 12h", "set Clockmode to 12h") );
136 }
137 }
138
159 ClockTool &m_tool; 139 ClockTool &m_tool;
160}; 140};
161 141