Skip to content

Commit

Permalink
Fixing messed stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
wizmer committed Jun 18, 2016
1 parent adbb502 commit 75496fc
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 58 deletions.
Expand Up @@ -26,16 +26,15 @@ public class OrgFile {

public String filename = "";
public String name = "";
public String checksum = "";

public boolean includeInOutline = true;
public long id = -1;
public long nodeId = -1;

public OrgFile() {
}

public OrgFile(String filename, String name, String checksum) {
this.checksum = checksum;
public OrgFile(String filename, String name) {
this.filename = filename;

if (name == null || name.equals("null"))
Expand Down Expand Up @@ -78,7 +77,6 @@ public void set(Cursor cursor) throws OrgFileNotFoundException {
cursor.moveToFirst();
this.name = cursor.getString(cursor.getColumnIndexOrThrow(Files.NAME));
this.filename = cursor.getString(cursor.getColumnIndexOrThrow(Files.FILENAME));
this.checksum = cursor.getString(cursor.getColumnIndexOrThrow(Files.CHECKSUM));
this.id = cursor.getLong(cursor.getColumnIndexOrThrow(Files.ID));
this.nodeId = cursor.getLong(cursor.getColumnIndexOrThrow(Files.NODE_ID));
} else {
Expand Down Expand Up @@ -130,7 +128,6 @@ private long addFileNode(long nodeId, ContentResolver resolver) {
ContentValues values = new ContentValues();
values.put(Files.FILENAME, filename);
values.put(Files.NAME, name);
values.put(Files.CHECKSUM, checksum);
values.put(Files.NODE_ID, nodeId);

Uri uri = resolver.insert(Files.CONTENT_URI, values);
Expand Down Expand Up @@ -181,18 +178,20 @@ public void updateFile(String content, Context context) {
}

/**
* Remove all OrgData nodes associated with this file from the DB
* Remove this OrgFile node from the DB
* 1) Remove all OrgNode(s) associated with this file from the DB
* 2) Remove this OrgFile node from the DB
* 3) Remove file from disk
*
* @param resolver
* @return the number of OrgData nodes removed
*/
public long removeFile(Context context) {
ContentResolver resolver = context.getContentResolver();
new File(getFilePath(context)).delete();

long entriesRemoved = removeFileOrgDataNodes(resolver);
removeFileNode(resolver);
new File(getFilePath(context)).delete();

return entriesRemoved;
}

Expand All @@ -202,9 +201,11 @@ private long removeFileNode(ContentResolver resolver) {
}

private long removeFileOrgDataNodes(ContentResolver resolver) {

int total = resolver.delete(OrgData.CONTENT_URI, OrgData.FILE_ID + "=?",
new String[]{Long.toString(id)});
total += resolver.delete(OrgData.buildIdUri(nodeId), null, null);
Log.v("sync","remove all nodes : "+total);
return total;
}

Expand Down
Expand Up @@ -158,7 +158,6 @@ public static void decryptAndParseFile(OrgFile orgFile, BufferedReader reader, C
intent.putExtra("data", FileUtils.read(reader).getBytes());
intent.putExtra("filename", orgFile.filename);
intent.putExtra("filenameAlias", orgFile.name);
intent.putExtra("checksum", orgFile.checksum);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
} catch (IOException e) {
Expand Down Expand Up @@ -193,7 +192,7 @@ private void init(OrgFile orgFile) {

this.position = new HashMap<>();
}

public void parse(OrgFile orgFile, BufferedReader breader, Context context) {
this.excludedTags = PreferenceUtils.getExcludedTags();

Expand All @@ -217,7 +216,7 @@ public void parse(OrgFile orgFile, BufferedReader breader) {
db.endTransaction();

}

private void parseLine(String line) {
if (TextUtils.isEmpty(line))
return;
Expand Down
Expand Up @@ -65,10 +65,12 @@ public static void add(String filename, Context context) {
}
}

public static HashSet<String> pull(Context context) {


public static SyncResult pull(final Context context) {
File repoDir = new File(context.getFilesDir() + "/" + GIT_DIR + "/.git");
Log.v("git", "pulling");
HashSet<String> result = new HashSet<>();
SyncResult result = new SyncResult();
try {
Git git = Git.open(repoDir);
Repository repository = git.getRepository();
Expand All @@ -91,12 +93,19 @@ public static HashSet<String> pull(Context context) {
.setOldTree(oldTreeIter)
.call();


for (DiffEntry entry : diffs) {
result.add(entry.getNewPath());
String newpath = entry.getNewPath();
String oldpath = entry.getOldPath();
Log.v("sync", "change old : "+oldpath + " -> "+newpath);
if(newpath.equals("/dev/null")){
result.deletedFiles.add(oldpath);
} else if(oldpath.equals("/dev/null")) {
result.newFiles.add(entry.getNewPath());
} else {
result.changedFiles.add(entry.getNewPath());
}
}


return result;
} catch (IOException e) {
e.printStackTrace();
} catch (DetachedHeadException e) {
Expand All @@ -118,10 +127,10 @@ public static HashSet<String> pull(Context context) {
} catch (GitAPIException e) {
e.printStackTrace();
}

return result;
}


static public class CloneGitRepoTask extends AsyncTask<String, Void, Object> {
Context context;
ProgressDialog progress;
Expand All @@ -130,11 +139,11 @@ public CloneGitRepoTask(Context context){
this.context = context;
}

protected Object doInBackground(String... params) {
protected Object doInBackground(String... params) {
AuthData authData = AuthData.getInstance(context);

File localPath = new File(context.getFilesDir() + "/" + GIT_DIR);
FileUtils.deleteFile(localPath);
File localPath = new File(context.getFilesDir() + "/" + GIT_DIR);
FileUtils.deleteFile(localPath);

// String REMOTE_URL = "ssh://" + userActual + ":" + passActual + "@" + hostActual + ":" + portActual + pathActual;
String REMOTE_URL = authData.getHost() + "/" + authData.getPath();
Expand All @@ -143,59 +152,59 @@ protected Object doInBackground(String... params) {
Log.v("git", "user home : " + System.getProperty("user.home"));
Log.v("git","after");

try {
git = Git.cloneRepository()
.setURI(REMOTE_URL)
.setDirectory(localPath)
try {
git = Git.cloneRepository()
.setURI(REMOTE_URL)
.setDirectory(localPath)
// .setCredentialsProvider(allowHosts)
.setCredentialsProvider(new CredentialsProviderAllowHost(authData.getUser(), authData.getPassword()))
.setBare(false)
.call();
} catch (Exception e) {
.call();
} catch (Exception e) {
e.printStackTrace();
return e;
}
return e;
}

return null;
}
return null;
}

@Override
protected void onPreExecute() {
super.onPreExecute();
@Override
protected void onPreExecute() {
super.onPreExecute();
progress = new ProgressDialog(context);
progress.setMessage(context.getString(R.string.please_wait));
progress.setTitle(context.getString(R.string.signing_in));
progress.show();
}
}

@Override
protected void onPostExecute(Object exception) {
@Override
protected void onPostExecute(Object exception) {
OrgProviderUtils
.clearDB(context.getContentResolver());


parseAll();

progress.dismiss();
if (exception == null) {
Toast.makeText(context, "Synchronization successful !", Toast.LENGTH_LONG).show();
((Activity) context).finish();
return;
}

if(exception instanceof TransportException){
Toast.makeText(context, "Authentification failed", Toast.LENGTH_LONG).show();
}else if (exception instanceof InvalidRemoteException) {
Toast.makeText(context, "Path does not exist or is not a valid repository", Toast.LENGTH_SHORT).show();
}else if(exception instanceof UnableToPushException){
progress.dismiss();
if (exception == null) {
Toast.makeText(context, "Synchronization successful !", Toast.LENGTH_LONG).show();
((Activity) context).finish();
return;
}

if(exception instanceof TransportException){
Toast.makeText(context, "Authentification failed", Toast.LENGTH_LONG).show();
}else if (exception instanceof InvalidRemoteException) {
Toast.makeText(context, "Path does not exist or is not a valid repository", Toast.LENGTH_SHORT).show();
}else if(exception instanceof UnableToPushException){
// git config receive.denyCurrentBranch ignore
Toast.makeText(context, "Push test failed. Make sure the repository is bare.", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, exception.toString(), Toast.LENGTH_LONG).show();
Toast.makeText(context, "Push test failed. Make sure the repository is bare.", Toast.LENGTH_LONG).show();
}else{
Toast.makeText(context, exception.toString(), Toast.LENGTH_LONG).show();
((Exception)exception).printStackTrace();
}
}

}
}

void parseAll(){
SSHSynchronizer syncher = new SSHSynchronizer(context);
Expand All @@ -207,7 +216,7 @@ void parseAll(){
{
String filename = file[i].getName();
if(filename.equals(".git")) continue;
OrgFile orgFile = new OrgFile(filename, filename,"");
OrgFile orgFile = new OrgFile(filename, filename);
FileReader fileReader = null;
try {
fileReader = new FileReader(syncher.getAbsoluteFilesDir(context) + "/" + filename);
Expand Down
Expand Up @@ -89,10 +89,10 @@ public void connect() throws JSchException {
}
}

public HashSet<String> synchronize(){
HashSet<String> changedFiles = JGitWrapper.pull(context);
public SyncResult synchronize(){
SyncResult pullResult = JGitWrapper.pull(context);
new JGitWrapper.PushGitRepoTask(context).execute();
return changedFiles;
return pullResult;
}

public void putRemoteFile(String filename, String contents) throws IOException {
Expand Down
@@ -0,0 +1,7 @@
package com.matburt.mobileorg2.Synchronizers;

/**
* Created by bcoste on 18/06/16.
*/
public class SyncResult {
}

0 comments on commit 75496fc

Please sign in to comment.