/android/export-contacts

To get this branch, use:
bzr branch http://bzr.ed.am/android/export-contacts

« back to all changes in this revision

Viewing changes to src/am/ed/exportcontacts/VcardExporter.java

  • Committer: Tim Marston
  • Date: 2013-07-16 16:24:59 UTC
  • Revision ID: tim@ed.am-20130716162459-u5z1iogael46ukp3
separate vcards with a newline

Show diffs side-by-side

added added

removed removed

1
1
/*
2
2
 * Exporter.java
3
3
 *
4
 
 * Copyright (C) 2011 Tim Marston <tim@ed.am>
 
4
 * Copyright (C) 2011 to 2013 Tim Marston <tim@ed.am>
5
5
 *
6
6
 * This file is part of the Export Contacts program (hereafter referred
7
 
 * to as "this program"). For more information, see
 
7
 * to as "this program").  For more information, see
8
8
 * http://ed.am/dev/android/export-contacts
9
9
 *
10
10
 * This program is free software: you can redistribute it and/or modify
36
36
public class VcardExporter extends Exporter
37
37
{
38
38
        protected FileOutputStream _ostream = null;
 
39
        protected boolean _first_contact = true;
39
40
 
40
41
        public VcardExporter( Doit doit )
41
42
        {
163
164
                return buffer.toString();
164
165
        }
165
166
 
166
 
 
167
167
        @Override
168
168
        protected boolean exportContact( ContactData contact )
169
169
                throws AbortExportException
174
174
                if( contact.getPrimaryIdentifier() == null )
175
175
                        return false;
176
176
 
 
177
                // append newline
 
178
                if( _first_contact )
 
179
                        _first_contact = false;
 
180
                else
 
181
                        out.append( "\n" );
 
182
 
177
183
                // append header
178
184
                out.append( "BEGIN:VCARD\n" );
179
185
                out.append( "VERSION:3.0\n" );
268
274
                                case ContactData.TYPE_WORK:
269
275
                                        types.add( "WORK" ); break;
270
276
                                }
 
277
                                // we use LABEL because is accepts formatted text (whereas ADR
 
278
                                // expects semicolon-delimited fields with specific purposes)
271
279
                                out.append( fold( "LABEL" +
272
280
                                        ( types.size() > 0? ";TYPE=" + join( types, "," ) : "" ) +
273
281
                                        ":" + escape( addresses.get( a ).getAddress() ) ) + "\n" );
283
291
                // append footer
284
292
                out.append( "END:VCARD\n" );
285
293
 
 
294
                // replace '\n' with "\r\n" (spec requires CRLF)
 
295
                int pos = 0;
 
296
                while( true ) {
 
297
                        pos = out.indexOf( "\n", pos );
 
298
                        if( pos == -1 ) break;
 
299
                        out.replace( pos, pos + 1, "\r\n" );
 
300
 
 
301
                        // skip our inserted string
 
302
                        pos += 2;
 
303
                }
 
304
 
286
305
                // write to file
287
306
                try {
288
307
                        _ostream.write( out.toString().getBytes() );