Make user to view top-level items and loads subitems in background.

This commit is contained in:
Anton Stankevich 2024-10-03 22:14:01 +00:00
parent 995ad46276
commit 5c47a8550a
4 changed files with 109 additions and 8 deletions

73
App.cpp
View File

@ -215,13 +215,13 @@ void recursive_file_tree(LIBMTP_mtpdevice_t *device,
printf(" ");
}
printf("%u %s\n", file->item_id, file->filename);*/
mainwin->listView()->AddItem(new FileItem(device,
mainwin->listView()->AddItem(new FileItem(device, storage,
file->item_id,
file->filename,
file->filetype != LIBMTP_FILETYPE_FOLDER,
depth));
if (file->filetype == LIBMTP_FILETYPE_FOLDER) {
recursive_file_tree(device, storage, file->item_id, depth+2);
recursive_file_tree(device, storage, file->item_id, depth+1);
}
}
oldfile = file;
@ -230,6 +230,51 @@ void recursive_file_tree(LIBMTP_mtpdevice_t *device,
}
}
void file_list(LIBMTP_mtpdevice_t *device,
LIBMTP_devicestorage_t *storage,
uint32_t leaf,
int depth,
BListItem * parentItem = NULL)
{
LIBMTP_file_t *files;
LIBMTP_file_t *file;
files = LIBMTP_Get_Files_And_Folders(device,
storage->id,
leaf);
if (files == NULL) {
return;
}
/* Iterate over the filelisting */
file = files;
while (file != NULL) {
LIBMTP_file_t *oldfile;
if (strcmp(file->filename, "Android")!=0) //skip system folder
{
FileItem * newItem = new FileItem(device, storage,
file->item_id,
file->filename,
file->filetype != LIBMTP_FILETYPE_FOLDER,
depth);
if (parentItem == NULL) {
mainwin->listView()->AddItem(newItem);
} else {
mainwin->listView()->AddUnder(newItem, parentItem);
}
/*
if (file->filetype == LIBMTP_FILETYPE_FOLDER) {
recursive_file_tree(device, storage, file->item_id, depth+1);
}*/
}
oldfile = file;
file = file->next;
LIBMTP_destroy_file_t(oldfile);
}
}
int init_mtp (/*int argc, char **argv*/)
{
LIBMTP_raw_device_t * rawdevices;
@ -299,7 +344,8 @@ int init_mtp (/*int argc, char **argv*/)
/* Loop over storages */
for (storage = device->storage; storage != 0; storage = storage->next) {
fprintf(stdout, "Storage: %s\n", storage->StorageDescription);
recursive_file_tree(device, storage, LIBMTP_FILES_AND_FOLDERS_ROOT, 0);
//recursive_file_tree(device, storage, LIBMTP_FILES_AND_FOLDERS_ROOT, 0);
file_list (device, storage, LIBMTP_FILES_AND_FOLDERS_ROOT, 0);
}
continue; //do not release device because later use
bailout:
@ -321,6 +367,27 @@ App::App(void)
mainwin->Show();
}
void App::AppActivated(bool active) {
fprintf(stdout, "App activated\n");
}
bool add_sub_files (BListItem * item, void* arg) {
FileItem * fileItem =((FileItem * ) item );
if (fileItem->isFolder() && mainwin->listView()->CountItemsUnder(item, true) == 0) {
fprintf(stdout, "Getting sub files for %d\n", fileItem->fileId());
file_list(fileItem->device(), fileItem->storage(), fileItem->fileId(), fileItem->OutlineLevel() +1, fileItem);
}
return false;
}
void App::ReadyToRun() {
fprintf(stdout, "App ready to run\n");
mainwin->listView()->DoForEach(&add_sub_files, NULL);
mainwin->listView()->FullListDoForEach(&add_sub_files, NULL);
}
int
main(void)

2
App.h
View File

@ -7,6 +7,8 @@ class App : public BApplication
{
public:
App(void);
void AppActivated(bool active);
void ReadyToRun();
};
#endif

View File

@ -11,10 +11,19 @@
//extern LIBMTP_mtpdevice_t * detected_device;
//extern App *app;
//extern LIBMTP_mtpdevice_t *detected_device=0;
extern MainWindow *mainwin;
int sendfile_function(char * from_path, int32_t parent_id, int32_t & ret_newFileID);
int progress (uint64_t const sent, uint64_t const total,
void const * const data);
void file_list(LIBMTP_mtpdevice_t *device,
LIBMTP_devicestorage_t *storage,
uint32_t leaf,
int depth,
BListItem * parentItem = NULL);
enum
{
M_DOWNLOAD_FILE = 'dlfl',
@ -22,14 +31,32 @@ enum
M_SET_TITLE = 'sttl'
};
FileItem::FileItem(LIBMTP_mtpdevice_t * device, int id, BString filename, bool isFile, int depth)
FileItem::FileItem(LIBMTP_mtpdevice_t * device, LIBMTP_devicestorage_t *storage, int id, BString filename, bool isFile, int depth)
:BStringItem(filename, depth, false)
{
this->fDevice=device;
this->fStorage = storage;
this->fId=id;
this->fIsFile=isFile;
}
inline BListItem * add_sub_files_under (BListItem * item, void* arg) {
FileItem * fileItem =((FileItem * ) item );
if (fileItem->isFolder()) {
fprintf(stdout, "Getting sub files for %d\n", fileItem->fileId());
file_list(fileItem->device(), fileItem->storage(), fileItem->fileId(), fileItem->OutlineLevel() +1, fileItem);
}
return item;
}
void FileItem::SetExpanded(bool expanded) {
BStringItem::SetExpanded(expanded);
if (expanded) {
//This code newer called
//fprintf(stdout, "Expanded!!!\n");
//mainwin->listView()->EachItemUnder(this, true, &add_sub_files_under, NULL);
}
}
MainWindow::MainWindow(void)
@ -177,7 +204,7 @@ MainWindow::MessageReceived(BMessage *msg)
case B_REFS_RECEIVED:{
printf(" User select Open\n" );
if (selectedItem) {
if (selectedItem) {
entry_ref ref;
if (msg->FindRef("refs", &ref) == B_OK) {
BEntry entry;
@ -193,8 +220,9 @@ MainWindow::MessageReceived(BMessage *msg)
if (send_res == 0)
{
FileItem * newFileItem = new FileItem(selectedItem->device(), newFileId, path.Leaf(), true, selectedItem->OutlineLevel() +1);
listView()->AddItem(newFileItem);
FileItem * newFileItem = new FileItem(selectedItem->device(),selectedItem->storage(),
newFileId, path.Leaf(), true, selectedItem->OutlineLevel() +1);
listView()->AddUnder(newFileItem, selectedItem);
printf("File added to listView\n" );
}
else

View File

@ -10,14 +10,18 @@
class FileItem: public BStringItem
{
public:
FileItem(LIBMTP_mtpdevice_t * device, int id, BString filename, bool isFile, int depth);
FileItem(LIBMTP_mtpdevice_t * device, LIBMTP_devicestorage_t * storage, int id, BString filename, bool isFile, int depth);
inline int fileId(){return fId;}
inline bool isFile(){return fIsFile;}
inline bool isFolder(){return !fIsFile;}
inline LIBMTP_mtpdevice_t * device(){return this->fDevice;}
inline LIBMTP_devicestorage_t * storage() {return this->fStorage;}
void SetExpanded(bool expanded);
private:
int fId;
bool fIsFile;
LIBMTP_mtpdevice_t * fDevice;
LIBMTP_devicestorage_t *fStorage;
};
class MainWindow : public BWindow