~ubuntu-branches/ubuntu/maverick/notecase/maverick

« back to all changes in this revision

Viewing changes to src/_win/RegisterExtension.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Cyril Brulebois
  • Date: 2005-09-09 09:32:43 UTC
  • Revision ID: james.westby@ubuntu.com-20050909093243-s6namw0yh7q3tqy0
Tags: upstream-1.0.5
ImportĀ upstreamĀ versionĀ 1.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
////////////////////////////////////////////////////////////////////////////
 
2
// NoteCase notes manager project <http://notecase.sf.net>
 
3
//
 
4
// This code is licensed under BSD license.See "license.txt" for more details.
 
5
//
 
6
// File: Class to register document format (*.ext) to be handled by given application 
 
7
////////////////////////////////////////////////////////////////////////////
 
8
 
 
9
#include "RegisterExtension.h"
 
10
#include <windows.h>
 
11
 
 
12
RegisterFileExtension::RegisterFileExtension()
 
13
{
 
14
        ClearData();
 
15
}
 
16
 
 
17
RegisterFileExtension::~RegisterFileExtension()
 
18
{
 
19
}
 
20
 
 
21
void RegisterFileExtension::ClearData()
 
22
{
 
23
        m_strName        = "";
 
24
        m_strExt         = "";
 
25
        m_strDesc        = "";
 
26
        m_strIconApp = "";
 
27
        m_nIconIndex = -1;
 
28
        m_lstCmds.clear();
 
29
}
 
30
 
 
31
void RegisterFileExtension::AddShellAction(const char *szName, const char *szCmd)
 
32
 
33
        //encode both string into one
 
34
        std::string strData(szName);
 
35
        strData += "\t";
 
36
        strData += szCmd;
 
37
 
 
38
        //store it
 
39
        m_lstCmds.push_back(strData);
 
40
}
 
41
 
 
42
void RegisterFileExtension::UpdateRegistry()
 
43
{
 
44
        Key_CreateRoot(m_strExt.c_str(),  "", m_strName.c_str());       //write ext key
 
45
        Key_CreateRoot(m_strName.c_str(), "", m_strDesc.c_str());       //write description key
 
46
 
 
47
        if(m_nIconIndex != -1)
 
48
        {
 
49
                std::string strKey(m_strName);
 
50
                strKey += "\\DefaultIcon";
 
51
                
 
52
                char buffer[20];
 
53
                std::string strVal(m_strIconApp);
 
54
                strVal += ",";
 
55
                strVal += itoa(m_nIconIndex, buffer, 10);
 
56
 
 
57
                Key_CreateRoot(strKey.c_str(), "", strVal.c_str());             //write default icon
 
58
        }
 
59
 
 
60
        for(int i=0; i<m_lstCmds.size(); i++)
 
61
        {
 
62
                int nDivider = m_lstCmds[i].find("\t");
 
63
                
 
64
                std::string strKey(m_strName);
 
65
                strKey += "\\Shell\\";
 
66
                strKey += m_lstCmds[i].substr(0, nDivider);
 
67
 
 
68
                //TOFIX action can have a description
 
69
                Key_CreateRoot(strKey.c_str(), "", "");                                 //write action name
 
70
 
 
71
                strKey += "\\command";
 
72
                Key_CreateRoot(strKey.c_str(), "", m_lstCmds[i].substr(nDivider+1,2000).c_str());               //write action command subkey
 
73
        }
 
74
 
 
75
        //sett default shell action - wrtie action name under "/Shell/"
 
76
        if(!m_strDefaultCmd.empty())
 
77
        {
 
78
                std::string strKey(m_strName);
 
79
                strKey += "\\Shell\\";
 
80
                
 
81
                Key_CreateRoot(strKey.c_str(), "", m_strDefaultCmd.c_str()); //write action name
 
82
        }
 
83
}
 
84
 
 
85
//create registry key, and write name/value pair (of string type) into it
 
86
//registry path starts at root (hardcoded limit), and can be up to 4 levels deep (RegCreateKey limitation)
 
87
bool RegisterFileExtension::Key_CreateRoot(const char *szPath, const char *szName, const char *szValue)
 
88
{
 
89
        HKEY    hBase = HKEY_CLASSES_ROOT;      // handle to the parent key
 
90
        HKEY    hKey;                                           // handle to the open registry key
 
91
 
 
92
        LONG lRes = RegCreateKey(hBase, szPath, &hKey);
 
93
        if(lRes == ERROR_SUCCESS)
 
94
        {
 
95
                //write the key/value
 
96
                if( NULL != szValue && 
 
97
                        '\0' != *szValue)
 
98
                {
 
99
                        RegSetValue(hKey, szName, REG_SZ, szValue, strlen(szValue));
 
100
                }
 
101
 
 
102
                RegCloseKey(hKey);
 
103
                return true;
 
104
        }
 
105
 
 
106
        return false;
 
107
}
 
108
 
 
109
bool RegisterFileExtension::IsRegistered(const char *szExt)
 
110
{
 
111
        HKEY hKey; // handle to the open registry key
 
112
        if(ERROR_SUCCESS == RegOpenKey(HKEY_CLASSES_ROOT, szExt, &hKey))
 
113
        {
 
114
                RegCloseKey(hKey);
 
115
                return true;
 
116
        }
 
117
        return false;
 
118
}