You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MRU.h 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* 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. #define WIN32_LEAN_AND_MEAN
  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. namespace rfb {
  28. namespace win32 {
  29. namespace MRU {
  30. static const RegKey RegRoot = HKEY_CURRENT_USER;
  31. static const TCHAR* RegPath = _T("Software\\TightVNC\\VNCViewer4\\MRU");
  32. static const int MaxMRUEntries = 256;
  33. static const int MRUEntries = 10;
  34. static std::list<char*> getEntries() {
  35. std::list<char*> mru;
  36. try {
  37. RegKey key;
  38. key.openKey(RegRoot, RegPath);
  39. CharArray order;
  40. int length;
  41. key.getBinary(_T("Order"), (void**)&order.buf, &length);
  42. for (int i=0; i<length; i++) {
  43. TCharArray keyname = rdr::HexOutStream::binToHexStr(&order.buf[i], 1);
  44. try {
  45. TCharArray entry = key.getString(keyname.buf);
  46. mru.push_back(strDup(entry.buf));
  47. } catch (rdr::Exception) {
  48. }
  49. }
  50. } catch (rdr::Exception) {
  51. }
  52. return mru;
  53. }
  54. static void addToMRU(const char* name) {
  55. RegKey key;
  56. key.createKey(RegRoot, RegPath);
  57. BYTE keycode;
  58. CharArray old_order;
  59. char order[MaxMRUEntries];
  60. int orderlen;
  61. try {
  62. key.getBinary(_T("Order"), (void**)&old_order.buf, &orderlen);
  63. if (orderlen)
  64. memcpy(order, old_order.buf, orderlen);
  65. std::set<int> ordercodes;
  66. keycode = 0;
  67. bool found = false;
  68. for (int i=0; i<orderlen; i++) {
  69. TCharArray keyname = rdr::HexOutStream::binToHexStr(&order[i], 1);
  70. try {
  71. TCharArray hostname = key.getString(keyname.buf);
  72. if (strcmp(name, CStr(hostname.buf)) == 0) {
  73. keycode = order[i];
  74. found = true;
  75. break;
  76. }
  77. } catch (rdr::Exception) {
  78. }
  79. ordercodes.insert(order[i]);
  80. }
  81. if (!found) {
  82. if (orderlen <= MRUEntries) {
  83. while (ordercodes.find(keycode) != ordercodes.end()) keycode++;
  84. } else {
  85. keycode = order[orderlen-1];
  86. orderlen--;
  87. }
  88. }
  89. } catch (rdr::Exception) {
  90. keycode = 0;
  91. orderlen = 0;
  92. }
  93. printf("keycode=%d\n", (int)keycode);
  94. orderlen++;
  95. int i, j=orderlen-1;
  96. for (i=0; i<orderlen-1; i++) {
  97. if (order[i] == keycode) {
  98. j = i;
  99. orderlen--;
  100. break;
  101. }
  102. }
  103. for (i=j; i>0; i--)
  104. order[i] = order[i-1];
  105. order[0] = keycode;
  106. printf("selected %d\n", (int)keycode);
  107. TCharArray keyname = rdr::HexOutStream::binToHexStr((char*)&keycode, 1);
  108. key.setString(keyname.buf, TStr(name));
  109. key.setBinary(_T("Order"), order, orderlen);
  110. }
  111. };
  112. };
  113. };
  114. #endif