001/*
002 * Shredzone Commons - pdb
003 *
004 * Copyright (C) 2009 Richard "Shred" Körber
005 *   http://commons.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU Library General Public License as
009 * published by the Free Software Foundation, either version 3 of the
010 * License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU Library General Public License
018 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019 */
020package org.shredzone.commons.pdb.converter;
021
022import java.io.IOException;
023
024import org.shredzone.commons.pdb.PdbDatabase;
025import org.shredzone.commons.pdb.PdbFile;
026import org.shredzone.commons.pdb.appinfo.CategoryAppInfo;
027import org.shredzone.commons.pdb.record.TodoRecord;
028
029/**
030 * A {@link Converter} that handles to-do records.
031 */
032public class TodoConverter implements Converter<TodoRecord, CategoryAppInfo> {
033
034    @Override
035    public boolean isAcceptable(PdbDatabase<TodoRecord, CategoryAppInfo> database) {
036        return "ToDoDB".equals(database.getName())
037                && "todo".equals(database.getCreator());
038    }
039
040    @Override
041    public TodoRecord convert(PdbFile reader, int record, int size, int attribute,
042            PdbDatabase<TodoRecord, CategoryAppInfo> database) throws IOException {
043        TodoRecord result = new TodoRecord(attribute);
044        if (result.isDelete()) {
045            return null;
046        }
047
048        result.setDate(reader.readPackedDate());
049
050        int flags = reader.readUnsignedByte();
051        result.setCompleted((flags & 0x80) != 0);
052        result.setPriority(flags & 0x7F);
053
054        result.setDescription(reader.readTerminatedString());
055
056        String note = reader.readTerminatedString();
057        if (note != null && note.length() > 0) {
058            result.setNote(note);
059        }
060
061        return result;
062    }
063
064    @Override
065    public CategoryAppInfo convertAppInfo(PdbFile reader, int size,
066            PdbDatabase<TodoRecord, CategoryAppInfo> database) throws IOException {
067        CategoryAppInfo result = new CategoryAppInfo();
068        reader.readCategories(result);
069        return result;
070    }
071
072}