~ubuntu-branches/ubuntu/maverick/newsbeuter/maverick

« back to all changes in this revision

Viewing changes to src/keymap.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Nico Golde
  • Date: 2007-04-21 19:44:35 UTC
  • Revision ID: james.westby@ubuntu.com-20070421194435-21g6134ws2yvarlt
Tags: upstream-0.3
ImportĀ upstreamĀ versionĀ 0.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#include <keymap.h>
 
2
#include <logger.h>
 
3
#include <vector>
 
4
#include <iostream>
 
5
#include <config.h>
 
6
 
 
7
namespace newsbeuter {
 
8
        
 
9
struct op_desc {
 
10
        operation op;
 
11
        char * help_text;
 
12
        unsigned short flags;
 
13
};
 
14
 
 
15
op_desc opdescs[] = {
 
16
        { OP_OPEN, _("Open feed/article"), KM_NEWSBEUTER },
 
17
        { OP_QUIT, _("Return to previous dialog/Quit"), KM_BOTH },
 
18
        { OP_RELOAD, _("Reload currently selected feed"), KM_NEWSBEUTER },
 
19
        { OP_RELOADALL, _("Reload all feeds"), KM_NEWSBEUTER },
 
20
        { OP_MARKFEEDREAD, _("Mark feed read"), KM_NEWSBEUTER },
 
21
        { OP_MARKALLFEEDSREAD, _("Mark all feeds read"), KM_NEWSBEUTER },
 
22
        { OP_SAVE, _("Save article"), KM_NEWSBEUTER },
 
23
        { OP_NEXTUNREAD, _("Go to next unread article"), KM_NEWSBEUTER },
 
24
        { OP_OPENINBROWSER, _("Open article in browser"), KM_NEWSBEUTER },
 
25
        { OP_HELP, _("Open help dialog"), KM_BOTH },
 
26
        { OP_TOGGLESOURCEVIEW, _("Toggle source view"), KM_NEWSBEUTER },
 
27
        { OP_TOGGLEITEMREAD, _("Toggle read status for article"), KM_NEWSBEUTER },
 
28
        { OP_TOGGLESHOWREAD, _("Toggle show read feeds"), KM_NEWSBEUTER },
 
29
        { OP_SHOWURLS, _("Show URLs in current article"), KM_NEWSBEUTER },
 
30
        { OP_CLEARTAG, _("Clear current tag"), KM_NEWSBEUTER },
 
31
        { OP_SETTAG, _("Select tag"), KM_NEWSBEUTER },
 
32
        { OP_SEARCH, _("Open search dialog"), KM_NEWSBEUTER },
 
33
        { OP_ENQUEUE, _("Add download to queue"), KM_NEWSBEUTER },
 
34
        { OP_PB_DOWNLOAD, _("Download file"), KM_PODBEUTER },
 
35
        { OP_PB_CANCEL, _("Cancel download"), KM_PODBEUTER },
 
36
        { OP_PB_DELETE, _("Mark download as deleted"), KM_PODBEUTER },
 
37
        { OP_PB_PURGE, _("Purge finished and deleted downloads from queue"), KM_PODBEUTER },
 
38
        { OP_PB_TOGGLE_DLALL, _("Toggle automatic download on/off"), KM_PODBEUTER },
 
39
        { OP_PB_PLAY, _("Start player with currently selected download"), KM_PODBEUTER },
 
40
        { OP_NIL, NULL }
 
41
};
 
42
 
 
43
keymap::keymap() { 
 
44
        keymap_["enter"] = OP_OPEN;
 
45
        keymap_["q"] = OP_QUIT;
 
46
        keymap_["r"] = OP_RELOAD;
 
47
        keymap_["R"] = OP_RELOADALL;
 
48
        keymap_["A"] = OP_MARKFEEDREAD;
 
49
        keymap_["C"] = OP_MARKALLFEEDSREAD;
 
50
        keymap_["s"] = OP_SAVE;
 
51
        keymap_["n"] = OP_NEXTUNREAD;
 
52
        keymap_["N"] = OP_TOGGLEITEMREAD;
 
53
        keymap_["o"] = OP_OPENINBROWSER;
 
54
        keymap_["?"] = OP_HELP;
 
55
        keymap_["^u"] = OP_TOGGLESOURCEVIEW;
 
56
        keymap_["l"] = OP_TOGGLESHOWREAD;
 
57
        keymap_["u"] = OP_SHOWURLS;
 
58
        keymap_["t"] = OP_SETTAG;
 
59
        keymap_["^t"] = OP_CLEARTAG;
 
60
        keymap_["/"] = OP_SEARCH;
 
61
        keymap_["e"] = OP_ENQUEUE;
 
62
 
 
63
        keymap_["d"] = OP_PB_DOWNLOAD;
 
64
        keymap_["c"] = OP_PB_CANCEL;
 
65
        keymap_["D"] = OP_PB_DELETE;
 
66
        keymap_["P"] = OP_PB_PURGE;
 
67
        keymap_["a"] = OP_PB_TOGGLE_DLALL;
 
68
        keymap_["+"] = OP_PB_MOREDL;
 
69
        keymap_["-"] = OP_PB_LESSDL;
 
70
        keymap_["p"] = OP_PB_PLAY;
 
71
 
 
72
        keymap_["NIL"] = OP_NIL;
 
73
}
 
74
 
 
75
void keymap::get_keymap_descriptions(std::vector<std::pair<std::string,std::string> >& descs, unsigned short flags) {
 
76
        for (std::map<std::string,operation>::iterator it=keymap_.begin();it!=keymap_.end();++it) {
 
77
                operation op = it->second;
 
78
                if (op != OP_NIL) {
 
79
                        std::string helptext;
 
80
                        bool add = false;
 
81
                        for (int i=0;opdescs[i].help_text;++i) {
 
82
                                if (opdescs[i].op == op && opdescs[i].flags & flags) {
 
83
                                        helptext = gettext(opdescs[i].help_text);
 
84
                                        add = true;
 
85
                                }
 
86
                        }
 
87
                        if (add) {
 
88
                                descs.push_back(std::pair<std::string,std::string>(it->first, helptext));
 
89
                        }
 
90
                }
 
91
        }
 
92
}
 
93
 
 
94
keymap::~keymap() { }
 
95
 
 
96
 
 
97
void keymap::set_key(operation op, const std::string& key) {
 
98
        GetLogger().log(LOG_DEBUG,"keymap::set_key(%d,%s) called", op, key.c_str());
 
99
        keymap_[key] = op;
 
100
}
 
101
 
 
102
void keymap::unset_key(const std::string& key) {
 
103
        GetLogger().log(LOG_DEBUG,"keymap::unset_key(%s) called", key.c_str());
 
104
        keymap_[key] = OP_NIL;  
 
105
}
 
106
 
 
107
operation keymap::get_opcode(const std::string& opstr) {
 
108
        struct { char * opstr; operation opcode; } opcode_map[] = {
 
109
                { "quit", OP_QUIT },
 
110
                { "reload", OP_RELOAD },
 
111
                { "reload-all", OP_RELOADALL },
 
112
                { "mark-feed-read", OP_MARKFEEDREAD },
 
113
                { "mark-all-feeds-read", OP_MARKALLFEEDSREAD },
 
114
                { "open", OP_OPEN },
 
115
                { "save", OP_SAVE },
 
116
                { "next-unread", OP_NEXTUNREAD },
 
117
                { "open-in-browser", OP_OPENINBROWSER },
 
118
                { "help", OP_HELP },
 
119
                { "toggle-source-view", OP_TOGGLESOURCEVIEW },
 
120
                { "toggle-article-read", OP_TOGGLEITEMREAD },
 
121
                { "toggle-show-read-feeds", OP_TOGGLESHOWREAD },
 
122
                { "enqueue", OP_ENQUEUE },
 
123
                { "show-urls", OP_SHOWURLS },
 
124
                { "clear-tag", OP_CLEARTAG },
 
125
                { "select-tag", OP_SETTAG },
 
126
                { "open-search", OP_SEARCH },
 
127
                { "pb-download", OP_PB_DOWNLOAD },
 
128
                { "pb-cancel", OP_PB_CANCEL },
 
129
                { "pb-delete", OP_PB_DELETE },
 
130
                { "pb-purge", OP_PB_PURGE },
 
131
                { "pb-toggle-download-all", OP_PB_TOGGLE_DLALL },
 
132
                { "pb-increase-max-dls", OP_PB_MOREDL },
 
133
                { "pb-decrease-max-dls", OP_PB_LESSDL },
 
134
                { "pb-play", OP_PB_PLAY },
 
135
                { NULL, OP_NIL }
 
136
        };
 
137
        for (int i=0;opcode_map[i].opstr;++i) {
 
138
                if (opstr == opcode_map[i].opstr) {
 
139
                        return opcode_map[i].opcode;
 
140
                }
 
141
        }
 
142
        return OP_NIL;
 
143
}
 
144
 
 
145
operation keymap::get_operation(const std::string& keycode) {
 
146
        std::string key;
 
147
        if (keycode.length() > 0) {
 
148
                if (keycode == "ENTER") {
 
149
                        key = "enter";
 
150
                } else if (keycode == "ESC") {
 
151
                        key = "esc";
 
152
                } else if (keycode[0] == 'F') {
 
153
                        key = keycode;
 
154
                        key[0] = 'f';
 
155
                } else if (strncmp(keycode.c_str(),"CHAR(",5)==0) {
 
156
                        unsigned int x;
 
157
                        char c;
 
158
                        sscanf(keycode.c_str(),"CHAR(%u)",&x);
 
159
                        // std::cerr << x << std::endl;
 
160
                        if (32 == x) {
 
161
                                key.append("space");
 
162
                        } else if (x > 32 && x <= 126) {
 
163
                                c = static_cast<char>(x);
 
164
                                key.append(1,c);
 
165
                        } else if (x >= 0 && x<=26) {
 
166
                                key.append("^");
 
167
                                key.append(1,static_cast<char>(0x60 + x));
 
168
                        } else {
 
169
                                // TODO: handle special keys
 
170
                        }
 
171
                }
 
172
        } else {
 
173
                key = "NIL";
 
174
        }
 
175
        return keymap_[key];
 
176
}
 
177
 
 
178
action_handler_status keymap::handle_action(const std::string& action, const std::vector<std::string>& params) {
 
179
        GetLogger().log(LOG_DEBUG,"keymap::handle_action(%s, ...) called",action.c_str());
 
180
        if (action == "bind-key") {
 
181
                if (params.size() < 2) {
 
182
                        return AHS_TOO_FEW_PARAMS;
 
183
                } else {
 
184
                        set_key(get_opcode(params[1]), params[0]);
 
185
                        // keymap_[params[0]] = get_opcode(params[1]);
 
186
                        return AHS_OK;
 
187
                }
 
188
        } else if (action == "unbind-key") {
 
189
                if (params.size() < 1) {
 
190
                        return AHS_TOO_FEW_PARAMS;
 
191
                } else {
 
192
                        unset_key(params[0]);
 
193
                        return AHS_OK;  
 
194
                }
 
195
        } else
 
196
                return AHS_INVALID_PARAMS;
 
197
}
 
198
 
 
199
std::string keymap::getkey(operation op) {
 
200
        for (std::map<std::string,operation>::iterator it=keymap_.begin(); it!=keymap_.end(); ++it) {
 
201
                if (it->second == op)
 
202
                        return it->first;       
 
203
        }       
 
204
        return "<none>";
 
205
}
 
206
 
 
207
 
 
208
}