rust_macios/contacts/
cn_mutable_contact.rs

1use objc::{msg_send, sel, sel_impl};
2
3use crate::{
4    foundation::{NSArray, NSData, NSDateComponents, NSString},
5    object,
6    objective_c_runtime::macros::interface_impl,
7};
8
9use super::{
10    CNContactRelation, CNInstantMessageAddress, CNLabeledValue, CNPhoneNumber, CNSocialProfile,
11    ICNContact, ICNPostalAddress,
12};
13
14object! {
15    /// A mutable object that stores information about a single contact, such as the contact's first name, phone numbers, and addresses.
16    unsafe pub struct CNMutableContact;
17}
18
19impl ICNContact for CNMutableContact {}
20
21#[interface_impl(CNContact)]
22impl CNMutableContact {
23    /// Sets the name prefix of the contact.
24    #[property]
25    pub fn set_name_prefix(&mut self, name_prefix: NSString) {
26        unsafe { msg_send![self.m_self(), setNamePrefix: name_prefix] }
27    }
28
29    /// Sets the given name of the contact.
30    #[property]
31    pub fn set_given_name(&mut self, given_name: NSString) {
32        unsafe { msg_send![self.m_self(), setGivenName: given_name] }
33    }
34
35    /// Sets the middle name of the contact.
36    #[property]
37    pub fn set_middle_name(&mut self, middle_name: NSString) {
38        unsafe { msg_send![self.m_self(), setMiddleName: middle_name] }
39    }
40
41    /// Sets the family name of the contact.
42    #[property]
43    pub fn set_family_name(&mut self, family_name: NSString) {
44        unsafe { msg_send![self.m_self(), setFamilyName: family_name] }
45    }
46
47    /// Sets the previous family name of the contact.
48    #[property]
49    pub fn set_previous_family_name(&mut self, previous_family_name: NSString) {
50        unsafe { msg_send![self.m_self(), setPreviousFamilyName: previous_family_name] }
51    }
52
53    /// Sets the name suffix of the contact.
54    #[property]
55    pub fn set_name_suffix(&mut self, name_suffix: NSString) {
56        unsafe { msg_send![self.m_self(), setNameSuffix: name_suffix] }
57    }
58
59    /// Sets the nickname of the contact.
60    #[property]
61    pub fn set_nickname(&mut self, nickname: NSString) {
62        unsafe { msg_send![self.m_self(), setNickname: nickname] }
63    }
64
65    /// Sets the phonetic given name of the contact.
66    #[property]
67    pub fn set_phonetic_given_name(&mut self, phonetic_given_name: NSString) {
68        unsafe { msg_send![self.m_self(), setPhoneticGivenName: phonetic_given_name] }
69    }
70
71    /// Sets the phonetic middle name of the contact.
72    #[property]
73    pub fn set_phonetic_middle_name(&mut self, phonetic_middle_name: NSString) {
74        unsafe { msg_send![self.m_self(), setPhoneticMiddleName: phonetic_middle_name] }
75    }
76
77    /// Sets the phonetic family name of the contact.
78    #[property]
79    pub fn set_phonetic_family_name(&mut self, phonetic_family_name: NSString) {
80        unsafe { msg_send![self.m_self(), setPhoneticFamilyName: phonetic_family_name] }
81    }
82
83    /* Setting Work Information
84     */
85
86    /// The contact’s job title.
87    #[property]
88    pub fn set_job_title(&mut self, job_title: NSString) {
89        unsafe { msg_send![self.m_self(), setJobTitle: job_title] }
90    }
91
92    /// The name of the department associated with the contact.
93    #[property]
94    pub fn set_department_name(&mut self, department_name: NSString) {
95        unsafe { msg_send![self.m_self(), setDepartmentName: department_name] }
96    }
97
98    /// The name of the organization associated with the contact.
99    #[property]
100    pub fn set_organization_name(&mut self, organization_name: NSString) {
101        unsafe { msg_send![self.m_self(), setOrganizationName: organization_name] }
102    }
103
104    /// The phonetic name of the organization associated with the contact.
105    #[property]
106    pub fn set_phonetic_organization_name(&mut self, phonetic_organization_name: NSString) {
107        unsafe {
108            msg_send![
109                self.m_self(),
110                setPhoneticOrganizationName: phonetic_organization_name
111            ]
112        }
113    }
114
115    /* Setting Addresses
116     */
117
118    /// An array of labeled postal addresses for a contact.
119    #[property]
120    pub fn set_postal_addresses<PostalAddress>(
121        &mut self,
122        postal_addresses: NSArray<CNLabeledValue<PostalAddress>>,
123    ) where
124        PostalAddress: ICNPostalAddress,
125    {
126        unsafe { msg_send![self.m_self(), setPostalAddresses: postal_addresses] }
127    }
128
129    /// An array of labeled email addresses for a contact.
130    #[property]
131    pub fn set_email_addresses(&mut self, email_addresses: NSArray<CNLabeledValue<NSString>>) {
132        unsafe { msg_send![self.m_self(), setEmailAddresses: email_addresses] }
133    }
134
135    /// An array of labeled URL addresses for a contact.
136    #[property]
137    pub fn set_url_addresses(&mut self, url_addresses: NSArray<CNLabeledValue<NSString>>) {
138        unsafe { msg_send![self.m_self(), setUrlAddresses: url_addresses] }
139    }
140
141    /* Setting Phone Information
142     */
143
144    /// An array of labeled phone numbers for a contact.
145    #[property]
146    pub fn set_phone_numbers(&mut self, phone_numbers: NSArray<CNLabeledValue<CNPhoneNumber>>) {
147        unsafe { msg_send![self.m_self(), setPhoneNumbers: phone_numbers] }
148    }
149
150    /* Setting Social Profiles
151     */
152
153    /// An array of labeled social profiles for a contact.
154    #[property]
155    pub fn set_social_profiles(
156        &mut self,
157        social_profiles: NSArray<CNLabeledValue<CNSocialProfile>>,
158    ) {
159        unsafe { msg_send![self.m_self(), setSocialProfiles: social_profiles] }
160    }
161
162    /* Setting Birthday Information
163     */
164
165    /// An array containing labeled Gregorian dates.
166    #[property]
167    pub fn set_dates(&mut self, dates: NSArray<CNLabeledValue<NSDateComponents>>) {
168        unsafe { msg_send![self.m_self(), setBirthdayDates: dates] }
169    }
170
171    /// A date component for the non-Gregorian birthday of the contact.
172    #[property]
173    pub fn set_non_gregorian_birthday(&mut self, non_gregorian_birthday: NSDateComponents) {
174        unsafe {
175            msg_send![
176                self.m_self(),
177                setNonGregorianBirthday: non_gregorian_birthday
178            ]
179        }
180    }
181
182    /// A date component for the Gregorian birthday of the contact.
183    #[property]
184    pub fn set_birthday(&mut self, birthday: NSDateComponents) {
185        unsafe { msg_send![self.m_self(), setBirthday: birthday] }
186    }
187
188    /* Setting Notes
189     */
190
191    /// A string containing notes for the contact.
192    #[property]
193    pub fn set_note(&mut self, note: NSString) {
194        unsafe { msg_send![self.m_self(), setNote: note] }
195    }
196
197    /// Setting Images
198    #[property]
199    pub fn set_image_data(&mut self, image_data: NSData) {
200        unsafe { msg_send![self.m_self(), setImageData: image_data] }
201    }
202
203    /* Relating Other Information to the Contact
204     */
205
206    /// An array of labeled contact relations for the contact.
207    #[property]
208    pub fn set_contact_relations(
209        &mut self,
210        contact_relations: NSArray<CNLabeledValue<CNContactRelation>>,
211    ) {
212        unsafe { msg_send![self.m_self(), setContactRelations: contact_relations] }
213    }
214
215    /// An array of labeled IM addresses for the contact.
216    #[property]
217    pub fn set_instant_messenger_addresses(
218        &mut self,
219        instant_messenger_addresses: NSArray<CNLabeledValue<CNInstantMessageAddress>>,
220    ) {
221        unsafe {
222            msg_send![
223                self.m_self(),
224                setInstantMessageAddresses: instant_messenger_addresses
225            ]
226        }
227    }
228}
229
230#[cfg(test)]
231mod test {
232    use crate::{
233        contacts::{CNContactType, CNMutableContact, ICNContact},
234        objective_c_runtime::traits::PNSObject,
235    };
236
237    #[test]
238    fn test_mutable_contact() {
239        let contact = CNMutableContact::m_new();
240
241        assert!(contact.p_birthday().is_none());
242        assert!(contact.p_contact_relations().count() == 0);
243        assert!(contact.p_contact_type() == CNContactType::Person);
244        assert!(contact.p_dates().count() == 0);
245        assert!(contact.p_department_name() == "");
246        assert!(contact.p_email_addresses().count() == 0);
247        assert!(contact.p_family_name() == "");
248        assert!(contact.p_given_name() == "");
249        assert!(contact.p_identifier() != "");
250        assert!(contact.p_image_data().is_none());
251        assert!(!contact.p_image_data_available());
252        assert!(contact.p_instant_messaging_addresses().count() == 0);
253        assert!(contact.p_job_title() == "");
254        assert!(contact.p_middle_name() == "");
255        assert!(contact.p_name_prefix() == "");
256        assert!(contact.p_name_suffix() == "");
257        assert!(contact.p_nickname() == "");
258        assert!(contact.p_non_gregorian_birthday().is_none());
259        assert!(contact.p_note() == "");
260        assert!(contact.p_organization_name() == "");
261        assert!(contact.p_phone_numbers().count() == 0);
262        assert!(contact.p_phonetic_given_name() == "");
263        assert!(contact.p_phonetic_middle_name() == "");
264        assert!(contact.p_phonetic_family_name() == "");
265        assert!(contact.p_postal_addresses().count() == 0);
266        assert!(contact.p_previous_family_name() == "");
267        assert!(contact.p_social_profiles().count() == 0);
268        assert!(contact.p_thumbnail_image_data().is_none());
269        assert!(contact.p_url_addresses().count() == 0);
270    }
271}