blob: 9e9939568c5b3eecae3bf0096b6a3fdfe5be6cfd [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2003 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18#ifndef __VIEWER_MRU_H__
19#define __VIEWER_MRU_H__
20
21#define WIN32_LEAN_AND_MEAN
22#include <windows.h>
23#include <list>
24#include <set>
25
26#include <rfb_win32/Registry.h>
27
28#include <rfb/util.h>
29#include <rdr/HexOutStream.h>
30
31namespace rfb {
32
33 namespace win32 {
34
35 namespace MRU {
36
37 static const RegKey RegRoot = HKEY_CURRENT_USER;
38 static const TCHAR* RegPath = _T("Software\\RealVNC\\VNCViewer4\\MRU");
39 static const int MaxMRUEntries = 256;
40 static const int MRUEntries = 10;
41
42 static std::list<char*> getEntries() {
43 std::list<char*> mru;
44
45 try {
46 RegKey key;
47 key.openKey(RegRoot, RegPath);
48
49 CharArray order;
50 int length;
51 key.getBinary(_T("Order"), (void**)&order.buf, &length);
52
53 for (int i=0; i<length; i++) {
54 TCharArray keyname = rdr::HexOutStream::binToHexStr(&order.buf[i], 1);
55 try {
56 TCharArray entry = key.getString(keyname.buf);
57 mru.push_back(strDup(entry.buf));
58 } catch (rdr::Exception) {
59 }
60 }
61 } catch (rdr::Exception) {
62 }
63
64 return mru;
65 }
66
67 static void addToMRU(const char* name) {
68 RegKey key;
69 key.createKey(RegRoot, RegPath);
70
71 BYTE keycode;
72 CharArray old_order;
73 char order[MaxMRUEntries];
74 int orderlen;
75
76 try {
77 key.getBinary(_T("Order"), (void**)&old_order.buf, &orderlen);
78 if (orderlen)
79 memcpy(order, old_order.buf, orderlen);
80
81 std::set<int> ordercodes;
82 keycode = 0;
83 bool found = false;
84 for (int i=0; i<orderlen; i++) {
85 TCharArray keyname = rdr::HexOutStream::binToHexStr(&order[i], 1);
86 try {
87 TCharArray hostname = key.getString(keyname.buf);
88 if (strcmp(name, CStr(hostname.buf)) == 0) {
89 keycode = order[i];
90 found = true;
91 break;
92 }
93 } catch (rdr::Exception) {
94 }
95 ordercodes.insert(order[i]);
96 }
97
98 if (!found) {
99 if (orderlen <= MRUEntries) {
100 while (ordercodes.find(keycode) != ordercodes.end()) keycode++;
101 } else {
102 keycode = order[orderlen-1];
103 orderlen--;
104 }
105 }
106
107 } catch (rdr::Exception) {
108 keycode = 0;
109 orderlen = 0;
110 }
111
112 printf("keycode=%d\n", (int)keycode);
113
114 orderlen++;
115 int i, j=orderlen-1;
116 for (i=0; i<orderlen-1; i++) {
117 if (order[i] == keycode) {
118 j = i;
119 orderlen--;
120 break;
121 }
122 }
123 for (i=j; i>0; i--)
124 order[i] = order[i-1];
125 order[0] = keycode;
126
127 printf("selected %d\n", (int)keycode);
128
129 TCharArray keyname = rdr::HexOutStream::binToHexStr((char*)&keycode, 1);
130 key.setString(keyname.buf, TStr(name));
131 key.setBinary(_T("Order"), order, orderlen);
132 }
133
134 };
135
136 };
137
138};
139
140#endif