Added folder create, rename, delete

Fixes #71
This commit is contained in:
M66B 2018-09-14 08:15:45 +00:00
parent d7e38c0f56
commit fc98eba42b
6 changed files with 212 additions and 28 deletions

View File

@ -114,12 +114,15 @@ public interface DaoFolder {
" AND type = :type")
int setFolderUser(long account, String type);
@Query("UPDATE folder SET synchronize = :synchronize, unified = :unified, after = :after WHERE id = :id")
int setFolderProperties(long id, boolean synchronize, boolean unified, int after);
@Query("UPDATE folder SET name = :name, synchronize = :synchronize, unified = :unified, after = :after WHERE id = :id")
int setFolderProperties(long id, String name, boolean synchronize, boolean unified, int after);
@Query("UPDATE folder SET name = :name WHERE account = :account AND name = :old")
int renameFolder(long account, String old, String name);
@Query("DELETE FROM folder WHERE id = :id")
void deleteFolder(long id);
@Query("DELETE FROM folder WHERE account= :account AND name = :name")
void deleteFolder(Long account, String name);
void deleteFolder(long account, String name);
}

View File

@ -20,31 +20,47 @@ package eu.faircode.email;
*/
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
import com.sun.mail.imap.IMAPFolder;
import com.sun.mail.imap.IMAPStore;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Session;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.Observer;
public class FragmentFolder extends FragmentEx {
private ViewGroup view;
private EditText etRename;
private CheckBox cbSynchronize;
private CheckBox cbUnified;
private EditText etAfter;
private Button btnSave;
private ImageButton ibDelete;
private ProgressBar pbSave;
private ProgressBar pbWait;
private long id = -1;
private long account = -1;
@Override
public void onCreate(Bundle savedInstanceState) {
@ -53,6 +69,7 @@ public class FragmentFolder extends FragmentEx {
// Get arguments
Bundle args = getArguments();
id = (args == null ? -1 : args.getLong("id"));
account = (args == null ? -1 : args.getLong("account"));
}
@Override
@ -63,11 +80,13 @@ public class FragmentFolder extends FragmentEx {
view = (ViewGroup) inflater.inflate(R.layout.fragment_folder, container, false);
// Get controls
etRename = view.findViewById(R.id.etRename);
cbSynchronize = view.findViewById(R.id.cbSynchronize);
cbUnified = view.findViewById(R.id.cbUnified);
etAfter = view.findViewById(R.id.etAfter);
pbSave = view.findViewById(R.id.pbSave);
btnSave = view.findViewById(R.id.btnSave);
ibDelete = view.findViewById(R.id.ibDelete);
pbSave = view.findViewById(R.id.pbSave);
pbWait = view.findViewById(R.id.pbWait);
btnSave.setOnClickListener(new View.OnClickListener() {
@ -75,34 +94,83 @@ public class FragmentFolder extends FragmentEx {
public void onClick(View v) {
Helper.setViewsEnabled(view, false);
btnSave.setEnabled(false);
ibDelete.setEnabled(false);
pbSave.setVisibility(View.VISIBLE);
Bundle args = new Bundle();
args.putLong("id", id);
args.putLong("account", account);
args.putString("name", etRename.getText().toString());
args.putBoolean("synchronize", cbSynchronize.isChecked());
args.putBoolean("unified", cbUnified.isChecked());
args.putString("after", etAfter.getText().toString());
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) {
protected Void onLoad(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
long aid = args.getLong("account");
String name = args.getString("name");
boolean synchronize = args.getBoolean("synchronize");
boolean unified = args.getBoolean("unified");
String after = args.getString("after");
int days = (TextUtils.isEmpty(after) ? 7 : Integer.parseInt(after));
int days = (TextUtils.isEmpty(after) ? EntityFolder.DEFAULT_USER_SYNC : Integer.parseInt(after));
IMAPStore istore = null;
DB db = DB.getInstance(getContext());
try {
db.beginTransaction();
db.folder().setFolderProperties(id, synchronize, unified, days);
if (!synchronize)
db.folder().setFolderError(id, null);
EntityFolder folder = db.folder().getFolder(id);
if (folder == null || !folder.name.equals(name)) {
EntityAccount account = db.account().getAccount(folder == null ? aid : folder.account);
Properties props = MessageHelper.getSessionProperties(context, account.auth_type);
Session isession = Session.getInstance(props, null);
istore = (IMAPStore) isession.getStore("imaps");
istore.connect(account.host, account.port, account.user, account.password);
if (folder == null) {
Log.i(Helper.TAG, "Creating folder=" + name);
IMAPFolder ifolder = (IMAPFolder) istore.getFolder(name);
if (ifolder.exists())
throw new IllegalArgumentException(getString(R.string.title_folder_exists, name));
ifolder.create(Folder.HOLDS_MESSAGES);
EntityFolder create = new EntityFolder();
create.account = aid;
create.name = name;
create.type = EntityFolder.USER;
create.unified = unified;
create.synchronize = synchronize;
create.after = days;
db.folder().insertFolder(create);
} else {
Log.i(Helper.TAG, "Renaming folder=" + name);
IMAPFolder iold = (IMAPFolder) istore.getFolder(folder.name);
IMAPFolder ifolder = (IMAPFolder) istore.getFolder(name);
if (ifolder.exists())
throw new IllegalArgumentException(getString(R.string.title_folder_exists, name));
iold.renameTo(ifolder);
}
}
if (folder != null) {
Log.i(Helper.TAG, "Updating folder=" + name);
db.folder().setFolderProperties(id, name, synchronize, unified, days);
if (!synchronize)
db.folder().setFolderError(id, null);
}
db.setTransactionSuccessful();
} finally {
db.endTransaction();
if (istore != null)
istore.close();
}
ServiceSynchronize.reload(getContext(), "save folder");
@ -119,17 +187,100 @@ public class FragmentFolder extends FragmentEx {
protected void onException(Bundle args, Throwable ex) {
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
ibDelete.setEnabled(true);
pbSave.setVisibility(View.GONE);
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentFolder.this, args);
}
});
ibDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new AlertDialog.Builder(getContext())
.setMessage(R.string.title_folder_delete)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Helper.setViewsEnabled(view, false);
btnSave.setEnabled(false);
ibDelete.setEnabled(false);
pbSave.setVisibility(View.VISIBLE);
Bundle args = new Bundle();
args.putLong("id", id);
new SimpleTask<Void>() {
@Override
protected Void onLoad(Context context, Bundle args) throws Throwable {
long id = args.getLong("id");
IMAPStore istore = null;
DB db = DB.getInstance(getContext());
try {
db.beginTransaction();
EntityFolder folder = db.folder().getFolder(id);
EntityAccount account = db.account().getAccount(folder.account);
Properties props = MessageHelper.getSessionProperties(context, account.auth_type);
Session isession = Session.getInstance(props, null);
istore = (IMAPStore) isession.getStore("imaps");
istore.connect(account.host, account.port, account.user, account.password);
IMAPFolder ifolder = (IMAPFolder) istore.getFolder(folder.name);
ifolder.delete(false);
db.folder().deleteFolder(id);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
if (istore != null)
istore.close();
}
ServiceSynchronize.reload(getContext(), "delete folder");
return null;
}
@Override
protected void onLoaded(Bundle args, Void data) {
getFragmentManager().popBackStack();
}
@Override
protected void onException(Bundle args, Throwable ex) {
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
ibDelete.setEnabled(true);
pbSave.setVisibility(View.GONE);
if (ex instanceof IllegalArgumentException)
Snackbar.make(view, ex.getMessage(), Snackbar.LENGTH_LONG).show();
else
Toast.makeText(getContext(), ex.toString(), Toast.LENGTH_LONG).show();
}
}.load(FragmentFolder.this, args);
}
})
.setNegativeButton(android.R.string.cancel, null)
.show();
}
});
// Initialize
Helper.setViewsEnabled(view, false);
btnSave.setEnabled(false);
ibDelete.setEnabled(false);
ibDelete.setVisibility(View.GONE);
pbSave.setVisibility(View.GONE);
pbWait.setVisibility(View.VISIBLE);
@ -146,25 +297,23 @@ public class FragmentFolder extends FragmentEx {
@Override
public void onChanged(@Nullable EntityFolder folder) {
if (folder == null) {
finish();
return;
}
if (once)
return;
once = true;
if (savedInstanceState == null) {
cbSynchronize.setChecked(folder.synchronize);
cbUnified.setChecked(folder.unified);
etAfter.setText(Integer.toString(folder.after));
etRename.setText(folder == null ? null : folder.name);
cbSynchronize.setChecked(folder == null ? true : folder.synchronize);
cbUnified.setChecked(folder == null ? false : folder.unified);
etAfter.setText(Integer.toString(folder == null ? EntityFolder.DEFAULT_USER_SYNC : folder.after));
}
// Consider previous save as cancelled
pbWait.setVisibility(View.GONE);
Helper.setViewsEnabled(view, true);
btnSave.setEnabled(true);
ibDelete.setEnabled(true);
ibDelete.setVisibility(folder == null ? View.GONE : View.VISIBLE);
}
});
}

View File

@ -19,7 +19,6 @@ package eu.faircode.email;
Copyright 2018 by Marcel Bokhorst (M66B)
*/
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
@ -36,6 +35,7 @@ import java.util.List;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.Group;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
@ -94,13 +94,13 @@ public class FragmentFolders extends FragmentEx {
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bundle args = getArguments();
long account = (args == null ? -1 : args.getLong("account"));
startActivity(new Intent(getContext(), ActivityCompose.class)
.putExtra("action", "new")
.putExtra("account", account)
);
Bundle args = new Bundle();
args.putLong("account", account);
FragmentFolder fragment = new FragmentFolder();
fragment.setArguments(args);
FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.content_frame, fragment).addToBackStack("folder");
fragmentTransaction.commit();
}
});

View File

@ -13,6 +13,26 @@
android:layout_height="match_parent"
android:padding="12dp">
<TextView
android:id="@+id/tvRename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="@string/title_folder_name"
android:textAppearance="@style/TextAppearance.AppCompat.Small"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/etRename"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:singleLine="true"
android:textAppearance="@style/TextAppearance.AppCompat.Medium"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tvRename" />
<CheckBox
android:id="@+id/cbSynchronize"
android:layout_width="wrap_content"
@ -20,7 +40,7 @@
android:layout_marginTop="12dp"
android:text="@string/title_synchronize_folder"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toBottomOf="@id/etRename" />
<CheckBox
android:id="@+id/cbUnified"
@ -62,6 +82,15 @@
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/etAfter" />
<ImageButton
android:id="@+id/ibDelete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:src="@drawable/baseline_delete_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/etAfter" />
<ProgressBar
android:id="@+id/pbSave"
style="@style/Base.Widget.AppCompat.ProgressBar"

View File

@ -80,7 +80,7 @@
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:src="@drawable/baseline_edit_24"
android:src="@drawable/baseline_add_24"
android:tint="@color/colorActionForeground"
app:backgroundTint="?attr/colorAccent"
app:layout_constraintBottom_toBottomOf="parent"

View File

@ -125,9 +125,12 @@
<string name="title_delete_local">Delete local messages</string>
<string name="title_edit_properties">Edit properties</string>
<string name="title_folder_name">Folder name</string>
<string name="title_synchronize_folder">Synchronize (receive messages)</string>
<string name="title_unified_folder">Show in unified inbox</string>
<string name="title_after">Synchronize (days)</string>
<string name="title_folder_exists">Folder %1$s exists</string>
<string name="title_folder_delete">Delete folder permanently?</string>
<string name="title_folder_unified">Unified inbox</string>
<string name="title_folder_inbox">Inbox</string>