public void parse(
      String filename,
      String filenameAlias,
      String checksum,
      BufferedReader breader,
      Context context) {
    this.useTitleField =
        PreferenceManager.getDefaultSharedPreferences(context).getBoolean("useAgendaTitle", false);

    this.file_id = addNewFile(filename, filenameAlias, checksum, true);

    this.todos = db.getGroupedTodos();

    this.starStack = new Stack<Integer>();
    this.parentIdStack = new Stack<Long>();

    this.starStack.push(0);
    Long fileID = db.getFileId(filename);
    this.parentIdStack.push(fileID);

    this.payload = new StringBuilder();

    db.getDB().beginTransaction();

    try {
      String currentLine;

      while ((currentLine = breader.readLine()) != null) {

        if (TextUtils.isEmpty(currentLine)) continue;

        int lineLength = currentLine.length();
        int numstars = numberOfStars(currentLine, lineLength);
        if (numstars > 0) {
          parseHeading(currentLine, numstars);
        } else {
          payload.append(currentLine);
          payload.append("\n");
        }
      }

      // Add payload to the final node
      db.addNodePayload(this.parentIdStack.peek(), this.payload.toString());

    } catch (IOException e) {
    }

    if (filename.equals("agendas.org")
        && PreferenceManager.getDefaultSharedPreferences(context)
            .getBoolean("combineBlockAgendas", false)
        && useTitleField) {
      combineBlockAgendas();
    }

    db.getDB().setTransactionSuccessful();
    db.getDB().endTransaction();

    updateCalendar(filename, context);
  }
  private void combineBlockAgendas() {
    final String filename = "agendas.org";
    long agendaFileNodeID = db.getFileId(filename);
    Cursor cursor = db.getNodeChildren(agendaFileNodeID);

    cursor.moveToFirst();

    String previousBlockTitle = "";
    long previousBlockNode = -1;

    while (cursor.isAfterLast() == false) {
      String name = cursor.getString(cursor.getColumnIndex("name"));

      if (name.indexOf(">") == -1) continue;

      String blockTitle = name.substring(0, name.indexOf(">"));

      if (TextUtils.isEmpty(blockTitle) == false) { // Is a block agenda

        if (blockTitle.equals(previousBlockTitle)
            == false) { // Create new node to contain block agenda
          previousBlockNode =
              db.addNode(agendaFileNodeID, blockTitle, "", "", "", db.getFilenameId(filename));
        }

        String blockEntryName = name.substring(name.indexOf(">") + 1);

        long nodeId = cursor.getLong(cursor.getColumnIndex("_id"));

        Cursor children = db.getNodeChildren(nodeId);
        children.moveToFirst();

        if (blockEntryName.startsWith("Day-agenda") && children.getCount() == 1) {
          blockEntryName = children.getString(children.getColumnIndex("name"));
          children = db.getNodeChildren(children.getLong(children.getColumnIndex("_id")));
          children.moveToFirst();
          cloneChildren(children, previousBlockNode, agendaFileNodeID, blockEntryName, filename);
        } else if (blockEntryName.startsWith("Week-agenda")) {
          while (children.isAfterLast() == false) {
            blockEntryName = children.getString(children.getColumnIndex("name"));
            Cursor children2 = db.getNodeChildren(children.getLong(children.getColumnIndex("_id")));
            children2.moveToFirst();
            cloneChildren(children2, previousBlockNode, agendaFileNodeID, blockEntryName, filename);
            children2.close();
            children.moveToNext();
          }
        } else
          cloneChildren(children, previousBlockNode, agendaFileNodeID, blockEntryName, filename);

        previousBlockTitle = blockTitle;
        db.deleteNode(cursor.getLong(cursor.getColumnIndex("_id")));
        children.close();
      }

      cursor.moveToNext();
    }

    cursor.close();
  }
Example #3
0
  @Override
  public Cursor query(
      Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
    final SQLiteDatabase db = dbHelper.getReadableDatabase();

    final SelectionBuilder builder = buildSelectionFromUri(uri);
    return builder.where(selection, selectionArgs).query(db, projection, sortOrder);
  }
Example #4
0
 @Override
 public int delete(Uri uri, String selection, String[] selectionArgs) {
   final SQLiteDatabase db = dbHelper.getWritableDatabase();
   final SelectionBuilder builder = buildSelectionFromUri(uri);
   int count = builder.where(selection, selectionArgs).delete(db);
   getContext().getContentResolver().notifyChange(uri, null);
   return count;
 }
  private void cloneChildren(
      Cursor children,
      long previousBlockNode,
      Long agendaNodeFileID,
      String blockEntryName,
      String filename) {
    db.addNode(
        previousBlockNode,
        BLOCK_SEPARATOR_PREFIX + blockEntryName,
        "",
        "",
        "",
        db.getFilenameId(filename));

    while (children.isAfterLast() == false) {
      db.cloneNode(
          children.getLong(children.getColumnIndex("_id")),
          previousBlockNode,
          db.getFilenameId("agendas.org"));
      children.moveToNext();
    }
  }
  private long parseLineIntoNode(String thisLine, int numstars) {
    String heading = thisLine.substring(numstars + 1);

    String name = "";
    String priority = "";
    String todo = "";
    String tags = "";

    Pattern pattern = prepareTitlePattern();
    Matcher matcher = pattern.matcher(heading);
    if (matcher.find()) {
      if (matcher.group(TODO_GROUP) != null) {
        String tempTodo = matcher.group(TODO_GROUP).trim();
        if (tempTodo.length() > 0 && isValidTodo(tempTodo)) {
          todo = tempTodo;
        } else {
          name = tempTodo + " ";
        }
      }
      if (matcher.group(PRIORITY_GROUP) != null) priority = matcher.group(PRIORITY_GROUP);

      name += matcher.group(TITLE_GROUP);

      if (this.useTitleField && matcher.group(AFTER_GROUP) != null) {
        int start = matcher.group(AFTER_GROUP).indexOf("TITLE:");
        int end = matcher.group(AFTER_GROUP).indexOf("</after>");

        if (start > -1 && end > -1) {
          String title = matcher.group(AFTER_GROUP).substring(start + 7, end);

          name = title + ">" + name;
        }
      }

      tags = matcher.group(TAGS_GROUP);
      if (tags == null) tags = "";

    } else {
      Log.w(LT, "Title not matched: " + heading);
      name = heading;
    }

    long nodeId = db.addNode(this.parentIdStack.peek(), name, todo, priority, tags, this.file_id);
    return nodeId;
  }
Example #7
0
  @Override
  public Uri insert(Uri uri, ContentValues contentValues) {
    Log.d(
        "MobileOrg/../OrgProvider",
        "Inserting to URI: " + uri + "  contentValues = " + contentValues);
    final String tableName = getTableNameFromUri(uri);

    if (contentValues == null) contentValues = new ContentValues();

    SQLiteDatabase db = dbHelper.getWritableDatabase();
    long rowId = db.insert(tableName, null, contentValues);

    if (rowId > 0) {
      Uri noteUri = ContentUris.withAppendedId(uri, rowId);
      getContext().getContentResolver().notifyChange(noteUri, null);
      return noteUri;
    } else throw new SQLException("Failed to insert row into " + uri);
  }
Example #8
0
 @Override
 public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
   Log.d(
       "MobileOrg/../OrgProvider",
       "Updating URI: "
           + uri
           + "  values = "
           + values
           + " selection: "
           + selection
           + " selectionArgs = "
           + selectionArgs);
   final SQLiteDatabase db = dbHelper.getWritableDatabase();
   final SelectionBuilder builder = buildSelectionFromUri(uri);
   int count = builder.where(selection, selectionArgs).update(db, values);
   getContext().getContentResolver().notifyChange(uri, null);
   return count;
 }
  private void parseHeading(String thisLine, int numstars) {
    db.addNodePayload(this.parentIdStack.peek(), this.payload.toString());

    this.payload = new StringBuilder();

    if (numstars == starStack.peek()) { // Node on same level
      starStack.pop();
      parentIdStack.pop();
    } else if (numstars < starStack.peek()) { // Node on lower level
      while (numstars <= starStack.peek()) {
        starStack.pop();
        parentIdStack.pop();
      }
    }

    long newId = parseLineIntoNode(thisLine, numstars);
    this.parentIdStack.push(newId);
    starStack.push(numstars);
  }
 private long addNewFile(
     String filename, String fileIdentActual, String remoteChecksum, boolean visable) {
   db.removeFile(filename);
   return db.addOrUpdateFile(filename, fileIdentActual, remoteChecksum, visable);
 }