blob: a0aef814de22574f98036caec22ed5f693c42687 [file] [log] [blame]
Constantin Kaplinsky729598c2006-05-25 05:12:25 +00001/* Copyright (C) 2002-2005 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#include <windows.h>
22#include <list>
23#include <set>
24#include <rfb_win32/Registry.h>
25#include <rfb/util.h>
26#include <rdr/HexOutStream.h>
27
28namespace rfb {
29
30 namespace win32 {
31
32 namespace MRU {
33
34 static const RegKey RegRoot = HKEY_CURRENT_USER;
Peter Åstrand4eacc022009-02-27 10:12:14 +000035 static const TCHAR* RegPath = _T("Software\\TigerVNC\\VNCViewer4\\MRU");
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000036 static const int MaxMRUEntries = 256;
37 static const int MRUEntries = 10;
38
39 static std::list<char*> getEntries() {
40 std::list<char*> mru;
41
42 try {
43 RegKey key;
44 key.openKey(RegRoot, RegPath);
45
46 CharArray order;
47 int length;
48 key.getBinary(_T("Order"), (void**)&order.buf, &length);
49
50 for (int i=0; i<length; i++) {
Peter Åstrandb22dbef2008-12-09 14:57:53 +000051 TCharArray keyname(rdr::HexOutStream::binToHexStr(&order.buf[i], 1));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000052 try {
Peter Åstrandb22dbef2008-12-09 14:57:53 +000053 TCharArray entry(key.getString(keyname.buf));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000054 mru.push_back(strDup(entry.buf));
55 } catch (rdr::Exception) {
56 }
57 }
58 } catch (rdr::Exception) {
59 }
60
61 return mru;
62 }
63
64 static void addToMRU(const char* name) {
65 RegKey key;
66 key.createKey(RegRoot, RegPath);
67
68 BYTE keycode;
69 CharArray old_order;
70 char order[MaxMRUEntries];
71 int orderlen;
72
73 try {
74 key.getBinary(_T("Order"), (void**)&old_order.buf, &orderlen);
75 if (orderlen)
76 memcpy(order, old_order.buf, orderlen);
77
78 std::set<int> ordercodes;
79 keycode = 0;
80 bool found = false;
81 for (int i=0; i<orderlen; i++) {
Peter Åstrandb22dbef2008-12-09 14:57:53 +000082 TCharArray keyname(rdr::HexOutStream::binToHexStr(&order[i], 1));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000083 try {
Peter Åstrandb22dbef2008-12-09 14:57:53 +000084 TCharArray hostname(key.getString(keyname.buf));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +000085 if (stricmp(name, CStr(hostname.buf)) == 0) {
86 keycode = order[i];
87 found = true;
88 break;
89 }
90 } catch (rdr::Exception) {
91 }
92 ordercodes.insert(order[i]);
93 }
94
95 if (!found) {
96 if (orderlen <= MRUEntries) {
97 while (ordercodes.find(keycode) != ordercodes.end()) keycode++;
98 } else {
99 keycode = order[orderlen-1];
100 orderlen--;
101 }
102 }
103
104 } catch (rdr::Exception) {
105 keycode = 0;
106 orderlen = 0;
107 }
108
109 orderlen++;
110 int i, j=orderlen-1;
111 for (i=0; i<orderlen-1; i++) {
112 if (order[i] == keycode) {
113 j = i;
114 orderlen--;
115 break;
116 }
117 }
118 for (i=j; i>0; i--)
119 order[i] = order[i-1];
120 order[0] = keycode;
121
Peter Åstrandb22dbef2008-12-09 14:57:53 +0000122 TCharArray keyname(rdr::HexOutStream::binToHexStr((char*)&keycode, 1));
Constantin Kaplinsky729598c2006-05-25 05:12:25 +0000123 key.setString(keyname.buf, TStr(name));
124 key.setBinary(_T("Order"), order, orderlen);
125 }
126
127 };
128
129 };
130
131};
132
133#endif