public void onDebugClick(View view) {
   Storage.getInstance().userID = 1;
   Storage.getInstance().firstname = "Nathaniel";
   Storage.getInstance().lastname = "Woodthorpe";
   Storage.getInstance().loginTime = System.currentTimeMillis();
   Storage.getInstance().email = "*****@*****.**";
   loggedIn();
 }
 public void end() {
   connector.close();
   documentManager.close();
   connector = null;
   documentManager = null;
   Storage.getInstance().getProjectNames().removeAll(Storage.getInstance().getProjectNames());
   Storage.getInstance().getUsers().removeAll(Storage.getInstance().getUsers());
 }
 private void populateProjectData(Project[] projects) {
   HashMap<String, Project> projectsList = new HashMap<String, Project>();
   ArrayList<String> projectNames = new ArrayList<String>();
   for (Project p : projects) {
     projectsList.put(p.getId(), p);
     projectNames.add(p.getName());
   }
   Storage.getInstance().setProjects(projectsList);
   Storage.getInstance().getProjectNames().addAll(projectNames);
   for (Project p : projects) {
     connector.fetchFiles(p.getId());
   }
 }
 /*v
  * (non-Javadoc)
  *
  * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
  */
 @Override
 protected void onPostExecute(String result) {
   String[] response = resp.split("&");
   if (response[0].equals("loggedin")) {
     Storage.getInstance().userID = Integer.parseInt(response[1]);
     Storage.getInstance().firstname = response[2];
     Storage.getInstance().lastname = response[3];
     Storage.getInstance().loginTime = System.currentTimeMillis();
     Storage.getInstance().email = emailBox.getText().toString();
     loggedIn();
   } else {
     System.out.println("NO BEANS");
     passwordBox.setError("Incorrect Login!");
     System.out.println(resp);
   }
 }
  public void begin() {
    //		System.out.println(ResourceManager.getWorkingDirectory());
    // Set up the core to listen to the CCWSC and the Document Manager
    if (connector != null || documentManager != null) end();
    connector = new CCWebSocketConnector();
    documentManager = new DocumentManager();

    ResourceManager.getFileMetadata();
    connector.addResponseNotificationListener(this);
    documentManager.addDocumentChangedListener(this);

    // get data from preferences
    Storage.getInstance()
        .setUsername(
            Activator.getDefault()
                .getPreferenceStore()
                .getString(
                    PreferenceConstants
                        .P_USERNAME)); // set("Username",Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.P_USERNAME));
    Storage.getInstance()
        .setPassword(
            Activator.getDefault()
                .getPreferenceStore()
                .getString(
                    PreferenceConstants
                        .P_PASSWORD)); // set("Password",Activator.getDefault().getPreferenceStore().getString(PreferenceConstants.P_PASSWORD));

    // TODO get data from metadata files
    // ResourceManager.getFileMetadata();

    connector.connect();
    sleep(1000);
    connector.login();
    sleep(1000);
    // documentManager.getActiveDocument();
    connector.fetchProjects();
    sleep(2000);
    // documentManager.generateDocumentsForProjects(); // generate document listeners
    connector.subscribe(documentManager.getActiveProjectId()); // change to current project
    documentManager.listen();
    sleep(1000);
    connector.pullDocument(documentManager.getActiveFileId());
    //        UIManager.showInfoDialog("Dank Title", "Insert Dank Memes Here");
    //    	Storage.getInstance().getUsers().add("jello");
    //    	Storage.getInstance().firePropertyChange("users", Storage.getInstance().getUsers(),
    // Storage.getInstance().getUsers());
  }
  public static void main(String[] args) {
    try {
      ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
      serverSocketChannel.socket().bind(new InetSocketAddress(4444));

      RoomManager rm = new RoomManager();
      Thread t = new Thread(rm);
      t.start();

      while (true) {
        SocketChannel socketChannel = serverSocketChannel.accept();
        ByteBuffer byteBuffer = ByteBuffer.allocate(256);
        byteBuffer.clear();
        int byteRead = socketChannel.read(byteBuffer);
        if (byteRead != -1) {
          byteBuffer.flip();
          String msg = "";
          while (byteBuffer.hasRemaining()) {
            msg += (char) byteBuffer.get();
          }
          if (msg.equals("save")) {
            System.out.println("saveaaa");
            Storage.getInstance().addSocketChannel(socketChannel);
            System.out.println("Socket aggiunto alla lista");
            byteBuffer.flip();
            byteBuffer.clear();
            byteBuffer.put(("Readed: " + msg).getBytes());
            byteBuffer.flip();
            socketChannel.write(byteBuffer);
          } else {
            System.out.println(msg);
            byteBuffer.flip();
            byteBuffer.clear();
            byteBuffer.put(("Readed: " + msg).getBytes());
            byteBuffer.flip();
            socketChannel.write(byteBuffer);
            socketChannel.close();
          }
        }
      }
    } catch (Exception e) {
      System.out.println(e.getMessage());
    }
  }
 private void interpretNotification(Notification n) {
   String action = n.getAction();
   String resource = n.getResource();
   switch (resource) {
     case "User":
       break;
     case "File":
       if (action.equals("Change")) {
         documentManager.recievePatch(n.getResId(), n.getData().getChanges());
       }
       break;
     case "Project":
       if (action.equals("Subscribe")) {
         Storage.getInstance().getUsers().add(n.getData().getUsername());
       }
       break;
     default:
       break;
   }
 }
 private void populateFileData(String projId, File[] files) {
   if (files == null) return;
   Project p = Storage.getInstance().getProjects().get(projId);
   if (p == null) return;
   p.setFiles(files);
 }
  private void interpretResponse(Response response, Request request) {
    if (response == null) {
      System.out.println("Failed to interpret response.");
      return;
    }
    if (request instanceof LoginRequest && response.getData() != null) {
      System.out.println("Retrieving user details...");
      Storage.getInstance()
          .setToken(response.getData().getToken()); // set("Token", response.getData().getToken());
      System.out.println("Token: " + Storage.getInstance().getToken()); // get("Token"));
    } else if (request instanceof PullFileRequest && response.getData() != null) {
      documentManager.flushFile(response.getData().getChanges(), response.getData().getBytes());
    } else if (request instanceof FetchProjectsRequest && response.getData() != null) {
      populateProjectData(response.getData().getProjects());
    } else if (request instanceof FetchFilesRequest && response.getData() != null) {
      populateFileData(request.getResId(), response.getData().getFiles());
    } else if (request instanceof SubscribeRequest && response.getStatus() == 1) {
      Storage.getInstance().getUsers().add(Storage.getInstance().getUsername() + " (me)");
    }
    switch (response.getStatus()) {
      case 1:
        return;
      case -100:
        UIManager.showInfoDialog("Error -100", "No such user found.");
        break; // no such user found error
      case -101:
        UIManager.showInfoDialog("Error -101", "Error creating user: internal error.");
        break; // error creating user: internal error
      case -102:
        UIManager.showInfoDialog("Error -102", "Error creating usre: duplicate username.");
        break; // error creating user: duplicate username (reprompt for new username)
      case -103:
        UIManager.showInfoDialog("Error -103", "Error logging in: internal error.");
        break; // error logging in: internal error
      case -104:
        UIManager.showInfoDialog("Error -104", "Error logging in: invalid username or password.");
        break; // error logging in: Invalid Username or Password
      case -105:
        UIManager.showInfoDialog("Error -105", "Error logging in: invalid token.");
        break; // listener.repromptLogin(); Error logging in: Invalid Token

      case -200:
        UIManager.showInfoDialog("Error -200", "No such project found.");
        break; // no such project found
      case -201:
        UIManager.showInfoDialog("Error -201", "Error creating project: internal error.");
        break; // error creating project: internal error
      case -202:
        UIManager.showInfoDialog("Error -202", "Error renaming project: internal error.");
        break; // error renaming project: internal error
      case -203:
        UIManager.showInfoDialog("Error -203", "Error granting permissions: internal error.");
        break; // error granting permissions: internal error
      case -204:
        UIManager.showInfoDialog("Error -204", "Error revoking permissions: internal error.");
        break; // error revoking permissions: internal error
      case -205:
        UIManager.showInfoDialog(
            "Error -205", "Error revoking permissions: document must have an owner.");
        break; // error revoking permissions: must have an owner
      case -206:
        UIManager.showInfoDialog("Error -206", "Error subscribing to project: internal error.");
        break; // error subscribing to project

      case -300:
        UIManager.showInfoDialog("Error -300", "No such file found.");
        break; // no such file found
      case -301:
        UIManager.showInfoDialog("Error -301", "Error creating file: internal error.");
        break; // error creating file: internal error
      case -302:
        UIManager.showInfoDialog("Error -302", "Error renaming file: internal error.");
        break; // error renaming file: internal error
      case -303:
        UIManager.showInfoDialog("Error -303", "Error moving file: internal error.");
        break; // error moving file: internal error
      case -304:
        UIManager.showInfoDialog("Error -304", "Error deleting file: internal error.");
        break; // error deleting file: internal error
      case -305:
        UIManager.showInfoDialog("Error -305", "Error creating file: file already exists.");
        break; // error creating file: duplicate file
      case -306:
        UIManager.showInfoDialog(
            "Error -306", "Error renaming file: file of new name already exists.");
        break; // error renaming file: duplicate file
      case -307:
        UIManager.showInfoDialog(
            "Error -307",
            "Error moving file: file of the same name exists in the target directory.");
        break; // error moving file: duplicate file
      case -308:
        UIManager.showInfoDialog("Error -308", "Error creating file: invalid file path.");
        break; // error creating file: invalid file path

      case -400:
        UIManager.showInfoDialog("Error -400", "Error inserting change: internal error.");
        break; // error inserting change: internal error
      case -401:
        connector.sendPatch(request.getResId(), request.getChanges());
        break; // UIManager.showInfoDialog("Error -401", "Error inserting change: duplicate version
        // number.");break; //error inserting change: duplicate version number
      case -402:
        UIManager.showInfoDialog("Error -402", "Error reading change: internal error.");
        break; // error reading change: internal error
      case -420:
        UIManager.showInfoDialog("Error -420", "Error, too blazed.");
        break; // error, too blazed
    }

    System.out.println("Successfully interpreted response of status: " + response.getStatus());
  }
 private void starText(final String clips) {
   Storage db = Storage.getInstance(this);
   db.changeClipStarStatus(clips);
 }