Add support for opening bitcoin: URIs directly.
[novacoin.git] / src / qt / qtipcserver.cpp
1 // Copyright (c) 2011 The Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4
5 #include <boost/algorithm/string.hpp>
6 #include <boost/interprocess/ipc/message_queue.hpp>
7 #include <boost/tokenizer.hpp>
8 #include <boost/date_time/posix_time/posix_time.hpp>
9
10 #include "headers.h"
11
12 using namespace boost::interprocess;
13 using namespace boost::posix_time;
14 using namespace boost;
15 using namespace std;
16
17 void ipcShutdown()
18 {
19     message_queue::remove("BitcoinURL");
20 }
21
22 void ipcThread(void* parg)
23 {
24     message_queue* mq = (message_queue*)parg;
25     char strBuf[257];
26     size_t nSize;
27     unsigned int nPriority;
28     loop
29     {
30         ptime d = boost::posix_time::microsec_clock::universal_time() + millisec(100);
31         if(mq->timed_receive(&strBuf, sizeof(strBuf), nSize, nPriority, d))
32         {
33             strBuf[nSize] = '\0';
34             // Convert bitcoin:// URLs to bitcoin: URIs
35             if (strBuf[8] == '/' && strBuf[9] == '/')
36             {
37                 for (int i = 8; i < 256; i++)
38                 {
39                     strBuf[i] = strBuf[i+2];
40                 }
41             }
42             ThreadSafeHandleURL(strBuf);
43             Sleep(1000);
44         }
45         if (fShutdown)
46         {
47             ipcShutdown();
48             break;
49         }
50     }
51     ipcShutdown();
52 }
53
54 void ipcInit()
55 {
56     message_queue* mq;
57     char strBuf[257];
58     size_t nSize;
59     unsigned int nPriority;
60     try {
61         mq = new message_queue(open_or_create, "BitcoinURL", 2, 256);
62
63         // Make sure we don't lose any bitcoin: URIs
64         for (int i = 0; i < 2; i++)
65         {
66             ptime d = boost::posix_time::microsec_clock::universal_time() + millisec(1);
67             if(mq->timed_receive(&strBuf, sizeof(strBuf), nSize, nPriority, d))
68             {
69                 strBuf[nSize] = '\0';
70                 // Convert bitcoin:// URLs to bitcoin: URIs
71                 if (strBuf[8] == '/' && strBuf[9] == '/')
72                 {
73                     for (int i = 8; i < 256; i++)
74                     {
75                         strBuf[i] = strBuf[i+2];
76                     }
77                 }
78                 ThreadSafeHandleURL(strBuf);
79             }
80             else
81                 break;
82         }
83
84         // Make sure only one bitcoin instance is listening
85         message_queue::remove("BitcoinURL");
86         mq = new message_queue(open_or_create, "BitcoinURL", 2, 256);
87     }
88     catch (interprocess_exception &ex) {
89         return;
90     }
91     if (!CreateThread(ipcThread, mq))
92     {
93         delete mq;
94     }
95 }