Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 216 lines (193 sloc) 7.073 kb
2f42024 @slegge Initial commit of BlackBerry Samples for Java
slegge authored
1 /*
2 * DesktopSample.cpp
3 *
4 * Copyright © 1998-2011 Research In Motion Limited
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Note: For the sake of simplicity, this sample application may not leverage
19 * resource bundles and resource strings. However, it is STRONGLY recommended
20 * that application developers make use of the localization features available
21 * within the BlackBerry development platform to ensure a seamless application
22 * experience across a variety of languages and geographies. For more information
23 * on localizing your application, please refer to the BlackBerry Java Development
24 * Environment Development Guide associated with this release.
25 */
26
27 // DesktopSample.cpp : Implementation of CDesktopSample
28 #include "stdafx.h"
29 #include "Desktop.h"
30 #include "DesktopSample.h"
31
32 /////////////////////////////////////////////////////////////////////////////
33 // CDesktopSample
34
35 CDesktopSample::CDesktopSample():
36 m_data(DATAFILE)
37 {
38
39 }
40
41 /*
42 * Implement the info query method - just return a simple string describing us
43 */
44 HRESULT __stdcall CDesktopSample::raw_GetExtensionInfo (
45 BSTR * extensionInfo )
46 {
47
48
49 ::SysReAllocString(extensionInfo, L"<?xml version=\"1.0\" encoding=\"UTF-8\" ?><extensioninfo version=\"1.0.0\">"
50 L"<vendorname>Rim</vendorname>"
51 L"<vendorversion>1.0</vendorversion>"
52 L"<path>C:\\Program Files\\Research In Motion\\BlackBerry SDK 2.5.0\\Desktop</path>"
53 L"<description>A Rim Desktop Extension API Sample</description><displayname>Rim Desktop Contacts Sample</displayname>"
54 L"<clsid>{7C23B673-FB1D-45D8-83BE-390398BA9876}</clsid>"
55 L"<access><database>Contacts</database></access>"
56 L"</extensioninfo>"); //the L token unicodes this string
57 return 0;
58 }
59
60 /*
61 * Act on the data!
62 * In this particular case we act on the sample database data, the database called "Contacts".
63 * Just do a simple synchronize operation based on a sample list of contacts, stored locally
64 * in a file.
65 */
66 HRESULT __stdcall CDesktopSample::raw_Process (
67 IRimUtility * pRimUtility,
68 IRimDatabaseAccess * pRimDeviceAccess )
69 {
70 try {
71 IRimUtilityPtr utilities = pRimUtility; //wrap in a smar pointer for ease of use
72 IRimProgressPtr progress = utilities; //this will throw an exception if the query fails
73 IRimDatabaseAccessPtr p = pRimDeviceAccess; //create a smart pointer for ease of use
74 IRimTablesPtr tables = p->GetTables();
75
76 //get the contacts table
77 IRimTablePtr contactTable;
ea58453 @slegge BlackBerry Samples for Java release 6.0.0.
slegge authored
78
79 long j;
80 tables->get_Count(&j);
81
2f42024 @slegge Initial commit of BlackBerry Samples for Java
slegge authored
82 for(long i=1; i<=tables->GetCount(); i++)
83 {
84 _bstr_t tableName = tables->GetItem(_variant_t(i))->GetName();
85 if(tableName == _bstr_t("Contacts"))
86 {
87 contactTable = tables->GetItem(_variant_t(i));
88 }
89 }
90
91 //sync from device to host, values in device replace those in host (just a simple one way sync)
92 // unless the list on the device is empty, in which case we reload the list from the host
93 //setup a progress dialog
94 progress->Notify(RIM_Progress_Show, 0, 0);
95 progress->SetProgressDlgText(RIM_ProgressText_Title, _bstr_t("Contacts Sample Progress"));
96 progress->SetProgressDlgText(RIM_ProgressText_Msg, _bstr_t("Processing records..."));
97 IRimRecordsPtr contacts = contactTable->LoadRecords(RIM_Mode_ReadWrite);
98
99 ContactList cl(DATAFILE);
ea58453 @slegge BlackBerry Samples for Java release 6.0.0.
slegge authored
100 long x = contacts->Count;
2f42024 @slegge Initial commit of BlackBerry Samples for Java
slegge authored
101 if ( contacts->Count <= 0 )
102 {
103 //populate the device with records from the store
104 int count = 0;
105 ContactList::Contact ** list = cl.get(&count);
106 progress->Notify(RIM_Progress_Count, count, 1);
107 for (int i = 0; i < count; ++i)
108 {
109 progress->Notify(RIM_Progress_Pos, i, 0);
110 IRimRecordPtr r = contacts->AddRecord();
111 if ( r != NULL )
112 {
113 IRimFieldsPtr fields = r->Getfields();
114 if ( fields != NULL )
115 {
116 AddField(fields, FIELDTAG_FIRST_NAME, list[i]->m_firstName);
117 AddField(fields, FIELDTAG_LAST_NAME, list[i]->m_lastName);
118 AddField(fields, FIELDTAG_EMAIL_ADDRESS, list[i]->m_email);
119 }
120 long id = r->Update();
121 id;
122
123 }
124 delete list[i];
125 list[i] = 0;
126 }
127 delete[] list;
128 }
129 else
130 {
131 progress->Notify(RIM_Progress_Count, contacts->Count, 1);
132 for (long i = 1; i <= contacts->Count; ++i) //the Item methods are 1 based indexed, rather than 0
133 {
134 IRimRecordPtr r = contacts->Item[i];
135 if ( r != NULL )
136 {
137 progress->Notify(RIM_Progress_Pos, i, 0);
138 IRimFieldsPtr fields = r->fields;
139
140 //we've unwrapped things here a bit to keep it easy to read
141 char * firstname = GetField(fields, FIELDTAG_FIRST_NAME);
142 char * lastname = GetField(fields, FIELDTAG_LAST_NAME);
143 char * email = GetField(fields, FIELDTAG_EMAIL_ADDRESS);
144 ContactList::Contact c(firstname, lastname, email);
145 cl.put(&c); //this will implicitly overwrite the data contained, if present (ie, if an element matches the first/last pair)
146 if ( firstname ) delete[] firstname;
147 if ( lastname ) delete[] lastname;
148 if ( email ) delete[] email;
149 }
150 }
151 cl.save(DATAFILE);
152 }
153
154
155 } catch (_com_error e) {
156 CString error;
157 error.Format("Exception in Desktop Sample [%d]:\n%s", e.Error() & 0xFFFF, (char *)e.Description());
158 ::MessageBox(NULL, (LPCTSTR)error, "Desktop Sample", MB_OK | MB_ICONERROR);
159 }
160 return 0;
161 }
162
163 char * CDesktopSample::GetField(IRimFieldsPtr fields, int fieldtag)
164 {
165 try {
166 IRimFieldPtr f = fields->FindField((char)fieldtag);
167 if ( f != NULL )
168 {
169 _variant_t data = f->value;
170 SAFEARRAY * sa = data.parray;
171 char * pdata;
172 char * retVal = NULL;
173 if ( SUCCEEDED( SafeArrayAccessData( sa, (void**)&pdata ) ) )
174 {
175 retVal = new char[strlen(pdata) + 1];
176 strcpy(retVal, pdata);
177 SafeArrayUnaccessData( sa );
178 }
179 return retVal;
180 }
181 } catch (_com_error e) {
182 //likely due to a fieldtag not found, so just swallow this error
183 }
184 return NULL;
185 }
186
187 void CDesktopSample::AddField(IRimFieldsPtr fields, int fieldtag, CString data)
188 {
189 IRimFieldPtr f = fields->AddField();
190 if ( NULL != f )
191 {
192 f->Id = (short)fieldtag;
193 f->value = _variant_t((LPCTSTR)data);
194 }
195 }
196
197 HRESULT __stdcall CDesktopSample::raw_Configure (
198 IRimUtility * pRimUtility,
199 long hWnd )
200 {
201 hWnd;
202 pRimUtility;
203 //no op
204 return 0;
205 }
206
207 HRESULT __stdcall CDesktopSample::raw_GetErrorString (
208 int errorCode,
209 BSTR * extensionInfo )
210 {
211 extensionInfo;
212 errorCode;
213 return 0;
214 }
215
Something went wrong with that request. Please try again.