Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 1 | /* 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 | |
| 28 | namespace rfb { |
| 29 | |
| 30 | namespace win32 { |
| 31 | |
| 32 | namespace MRU { |
| 33 | |
| 34 | static const RegKey RegRoot = HKEY_CURRENT_USER; |
Peter Åstrand | 4eacc02 | 2009-02-27 10:12:14 +0000 | [diff] [blame] | 35 | static const TCHAR* RegPath = _T("Software\\TigerVNC\\VNCViewer4\\MRU"); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 36 | 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 Åstrand | b22dbef | 2008-12-09 14:57:53 +0000 | [diff] [blame] | 51 | TCharArray keyname(rdr::HexOutStream::binToHexStr(&order.buf[i], 1)); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 52 | try { |
Peter Åstrand | b22dbef | 2008-12-09 14:57:53 +0000 | [diff] [blame] | 53 | TCharArray entry(key.getString(keyname.buf)); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 54 | 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 Åstrand | b22dbef | 2008-12-09 14:57:53 +0000 | [diff] [blame] | 82 | TCharArray keyname(rdr::HexOutStream::binToHexStr(&order[i], 1)); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 83 | try { |
Peter Åstrand | b22dbef | 2008-12-09 14:57:53 +0000 | [diff] [blame] | 84 | TCharArray hostname(key.getString(keyname.buf)); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 85 | 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 Åstrand | b22dbef | 2008-12-09 14:57:53 +0000 | [diff] [blame] | 122 | TCharArray keyname(rdr::HexOutStream::binToHexStr((char*)&keycode, 1)); |
Constantin Kaplinsky | 729598c | 2006-05-25 05:12:25 +0000 | [diff] [blame] | 123 | key.setString(keyname.buf, TStr(name)); |
| 124 | key.setBinary(_T("Order"), order, orderlen); |
| 125 | } |
| 126 | |
| 127 | }; |
| 128 | |
| 129 | }; |
| 130 | |
| 131 | }; |
| 132 | |
| 133 | #endif |