Skip to content

Commit

Permalink
fix: fileStatus中文
Browse files Browse the repository at this point in the history
  • Loading branch information
biezhihua committed Apr 4, 2021
1 parent 3408a8b commit c3ea67e
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,21 +255,52 @@ class MainActivity : AppCompatActivity() {
return ViewHolder(view)
}

var path = ""
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
val fileName = dataset[position] as? String
fileName?.let {
val itemData = dataset[position]
if (itemData is String) {
processShare(itemData, viewHolder)
} else if (itemData is JSONObject) {
val fileName = itemData.getString("name")
viewHolder.file_name.text = fileName
viewHolder.itemView.setOnClickListener {
val tid = dsm?.treeConnect(fileName)
if (tid != null && tid > 0) {
val result = dsm?.find(tid, "\\*")
Log.d(TAG, "onBindViewHolder() called : $result")
} else {
mainActivity.tip.duration = Snackbar.LENGTH_LONG
mainActivity.tip.setText("错误码 $tid")
mainActivity.tip.show()
if (tid != null) {
val fileStatus = dsm?.fileStatus(tid!!, path + "\\" + fileName)
val data = fileStatus?.getJSONObject("data")
val isDir = data?.getIntValue("is_dir")
if (isDir == 1) {
val dirName = data.getString("name")
val findNext = "$dirName\\*"
dsm?.find(tid!!, findNext)?.getJSONArray("data")?.let {
path += dirName
Log.d(TAG, "onBindViewHolder() called : $it")
setDatas(it)
}
}
Log.d(TAG, "onBindViewHolder() called: status = ${fileStatus}")
}
}
}
}

private var tid: Int? = null

private fun processShare(
shareName: String,
viewHolder: ViewHolder
) {
viewHolder.file_name.text = shareName
viewHolder.itemView.setOnClickListener {
tid = dsm?.treeConnect(shareName)
if (tid != null && tid!! > 0) {
dsm?.find(tid!!, "\\*")?.getJSONArray("data")?.let {
Log.d(TAG, "processShare() called : $it")
setDatas(it)
}
} else {
mainActivity.tip.duration = Snackbar.LENGTH_LONG
mainActivity.tip.setText("错误码 $tid")
mainActivity.tip.show()
}
}
}
Expand All @@ -278,15 +309,18 @@ class MainActivity : AppCompatActivity() {

fun addData(data: JSONObject) {
dataset.add(data)
notifyItemChanged(0, dataset.size)
notifyDataSetChanged()
}

fun addDatas(datas: JSONArray) {
dataset.addAll(datas)
notifyItemChanged(0, dataset.size)
notifyDataSetChanged()
}

fun setDatas(datas: JSONArray) {
dataset.clear()
dataset.addAll(datas)
notifyDataSetChanged()
}
}


}
9 changes: 6 additions & 3 deletions libdsm_android/src/main/java/open/android/lib/dsm/Dsm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,8 @@ class Dsm {
* @return An a json list.
*/
fun getShareList(): JSONObject? {
return JSONObject.parseObject(_shareGetListJson(this))
val result = _shareGetListJson(this)
return JSONObject.parseObject(result)
}

/**
Expand Down Expand Up @@ -180,7 +181,8 @@ class Dsm {
* @return An json list of files.
*/
fun find(tid: Int, pattern: String): JSONObject? {
return JSONObject.parseObject(_find(this, tid, pattern))
val result = _find(this, tid, pattern)
return JSONObject.parseObject(result)
}

/**
Expand All @@ -193,7 +195,8 @@ class Dsm {
* destory this object with smb_stat_destroy after usage.
*/
fun fileStatus(tid: Int, path: String): JSONObject? {
return JSONObject.parseObject(_fileStatus(this, tid, path))
val result = _fileStatus(this, tid, path)
return JSONObject.parseObject(result)
}

protected fun finalize() {
Expand Down
18 changes: 11 additions & 7 deletions libdsm_core/Dsm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,13 @@ string *Dsm::find(int tid, const char *pattern) {
json file;

// name
file["name"] = smb_stat_name(st);
const char *name = smb_stat_name(st);
file["name"] = name;

LOGD("[%s] find %s %s", __func__, pattern, name);

// 0 -> not a directory, != 0 -> directory
file["is_dir"] = (smb_stat_get(st, SMB_STAT_ISDIR) != 0 ? 1 : 0);
file["is_dir"] = smb_stat_get(st, SMB_STAT_ISDIR) != 0 ? 1 : 0;

// Get file size
file["size"] = smb_stat_get(st, SMB_STAT_SIZE);
Expand Down Expand Up @@ -336,12 +339,14 @@ string *Dsm::fileStatus(int tid, const char *path) {
smb_stat st = smb_fstat(session, smbTid, path);

json result;
json data = json::array();

json file;

// name
file["name"] = smb_stat_name(st);
const char *name = smb_stat_name(st);
// FIXME: 修复中文错误
file["name"] = path;

LOGD("[%s] fileStatus %s %s", __func__, path, name);

// 0 -> not a directory, != 0 -> directory
file["is_dir"] = (smb_stat_get(st, SMB_STAT_ISDIR) != 0 ? 1 : 0);
Expand All @@ -361,9 +366,8 @@ string *Dsm::fileStatus(int tid, const char *path) {
// Get file last moditification time
file["last_moditification_time"] = smb_stat_get(st, SMB_STAT_MTIME);

data.push_back(file);
smb_stat_destroy(st);

result["data"] = data;
result["data"] = file;
return new string(result.dump(-1, ' ', true, nlohmann::json::error_handler_t::replace));
}

0 comments on commit c3ea67e

Please sign in to comment.