1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=2 et sw=2 tw=80: */
3 : /* ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
6 : * The contents of this file are subject to the Mozilla Public License Version
7 : * 1.1 (the "License"); you may not use this file except in compliance with
8 : * the License. You may obtain a copy of the License at
9 : * http://www.mozilla.org/MPL/
10 : *
11 : * Software distributed under the License is distributed on an "AS IS" basis,
12 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 : * for the specific language governing rights and limitations under the
14 : * License.
15 : *
16 : * The Original Code is mozilla.org code.
17 : *
18 : * The Initial Developer of the Original Code is
19 : * Sun Microsystems, Inc.
20 : * Portions created by the Initial Developer are Copyright (C) 2002
21 : * the Initial Developer. All Rights Reserved.
22 : *
23 : * Contributor(s):
24 : * Evan Yan (evan.yan@sun.com)
25 : *
26 : * Alternatively, the contents of this file may be used under the terms of
27 : * either the GNU General Public License Version 2 or later (the "GPL"), or
28 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 : * in which case the provisions of the GPL or the LGPL are applicable instead
30 : * of those above. If you wish to allow use of your version of this file only
31 : * under the terms of either the GPL or the LGPL, and not to allow others to
32 : * use your version of this file under the terms of the MPL, indicate your
33 : * decision by deleting the provisions above and replace them with the notice
34 : * and other provisions required by the GPL or the LGPL. If you do not delete
35 : * the provisions above, a recipient may use your version of this file under
36 : * the terms of any one of the MPL, the GPL or the LGPL.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsMaiInterfaceDocument.h"
41 :
42 : #include "nsAccessibleWrap.h"
43 : #include "nsDocAccessible.h"
44 :
45 : static const char* const kDocTypeName = "W3C-doctype";
46 : static const char* const kDocUrlName = "DocURL";
47 : static const char* const kMimeTypeName = "MimeType";
48 :
49 : // below functions are vfuncs on an ATK interface so they need to be C call
50 : extern "C" {
51 :
52 : static const gchar* getDocumentLocaleCB(AtkDocument* aDocument);
53 : static AtkAttributeSet* getDocumentAttributesCB(AtkDocument* aDocument);
54 : static const gchar* getDocumentAttributeValueCB(AtkDocument* aDocument,
55 : const gchar* aAttrName);
56 :
57 : void
58 0 : documentInterfaceInitCB(AtkDocumentIface *aIface)
59 : {
60 0 : NS_ASSERTION(aIface, "Invalid Interface");
61 0 : if(!aIface)
62 0 : return;
63 :
64 : /*
65 : * We don't support get_document or set_attribute right now.
66 : * get_document_type is deprecated, we return DocType in
67 : * get_document_attribute_value and get_document_attributes instead.
68 : */
69 0 : aIface->get_document_attributes = getDocumentAttributesCB;
70 0 : aIface->get_document_attribute_value = getDocumentAttributeValueCB;
71 0 : aIface->get_document_locale = getDocumentLocaleCB;
72 : }
73 :
74 : const gchar *
75 0 : getDocumentLocaleCB(AtkDocument *aDocument)
76 : {
77 0 : nsAccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aDocument));
78 0 : if (!accWrap)
79 0 : return nsnull;
80 :
81 0 : nsAutoString locale;
82 0 : accWrap->Language(locale);
83 0 : return locale.IsEmpty() ? nsnull : nsAccessibleWrap::ReturnString(locale);
84 : }
85 :
86 : static inline GSList *
87 0 : prependToList(GSList *aList, const char *const aName, const nsAutoString &aValue)
88 : {
89 0 : if (aValue.IsEmpty())
90 0 : return aList;
91 :
92 : // libspi will free these
93 0 : AtkAttribute *atkAttr = (AtkAttribute *)g_malloc(sizeof(AtkAttribute));
94 0 : atkAttr->name = g_strdup(aName);
95 0 : atkAttr->value = g_strdup(NS_ConvertUTF16toUTF8(aValue).get());
96 0 : return g_slist_prepend(aList, atkAttr);
97 : }
98 :
99 : AtkAttributeSet *
100 0 : getDocumentAttributesCB(AtkDocument *aDocument)
101 : {
102 0 : nsAccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aDocument));
103 0 : if (!accWrap || !accWrap->IsDoc())
104 0 : return nsnull;
105 :
106 : // according to atkobject.h, AtkAttributeSet is a GSList
107 0 : GSList* attributes = nsnull;
108 0 : nsDocAccessible* document = accWrap->AsDoc();
109 0 : nsAutoString aURL;
110 0 : nsresult rv = document->GetURL(aURL);
111 0 : if (NS_SUCCEEDED(rv))
112 0 : attributes = prependToList(attributes, kDocUrlName, aURL);
113 :
114 0 : nsAutoString aW3CDocType;
115 0 : rv = document->GetDocType(aW3CDocType);
116 0 : if (NS_SUCCEEDED(rv))
117 0 : attributes = prependToList(attributes, kDocTypeName, aW3CDocType);
118 :
119 0 : nsAutoString aMimeType;
120 0 : rv = document->GetMimeType(aMimeType);
121 0 : if (NS_SUCCEEDED(rv))
122 0 : attributes = prependToList(attributes, kMimeTypeName, aMimeType);
123 :
124 0 : return attributes;
125 : }
126 :
127 : const gchar *
128 0 : getDocumentAttributeValueCB(AtkDocument *aDocument,
129 : const gchar *aAttrName)
130 : {
131 0 : nsAccessibleWrap* accWrap = GetAccessibleWrap(ATK_OBJECT(aDocument));
132 0 : if (!accWrap || !accWrap->IsDoc())
133 0 : return nsnull;
134 :
135 0 : nsDocAccessible* document = accWrap->AsDoc();
136 : nsresult rv;
137 0 : nsAutoString attrValue;
138 0 : if (!strcasecmp(aAttrName, kDocTypeName))
139 0 : rv = document->GetDocType(attrValue);
140 0 : else if (!strcasecmp(aAttrName, kDocUrlName))
141 0 : rv = document->GetURL(attrValue);
142 0 : else if (!strcasecmp(aAttrName, kMimeTypeName))
143 0 : rv = document->GetMimeType(attrValue);
144 : else
145 0 : return nsnull;
146 :
147 0 : NS_ENSURE_SUCCESS(rv, nsnull);
148 0 : return attrValue.IsEmpty() ? nsnull : nsAccessibleWrap::ReturnString(attrValue);
149 : }
150 : }
|