filedialogextern.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include "filedialogextern.h"
  2. #include "simpleCustomEvent.h"
  3. #include <QEvent>
  4. #include <QCoreApplication>
  5. #ifdef ANDROID
  6. #include <QtAndroidExtras/QAndroidJniObject>
  7. #include <QFile>
  8. #include<unistd.h>
  9. //#include <QGuiApplication>
  10. #include <QAndroidJniEnvironment>
  11. #else
  12. #include <QFileDialog>
  13. #include <QDir>
  14. #endif
  15. #include <QMessageBox>
  16. #include <QDebug>
  17. #define DEBUG qDebug()<<__func__<<__LINE__
  18. static QObject *g_listener = 0;
  19. FileDialogExtern::FileDialogExtern(QObject *parent) : QObject(parent)
  20. {
  21. }
  22. bool FileDialogExtern::event(QEvent *e)
  23. {
  24. #ifdef ANDROID
  25. if(e->type() == SimpleCustomEvent::eventType())
  26. {
  27. SimpleCustomEvent *sce = (SimpleCustomEvent*)e;
  28. qDebug()<<"FileDialogExtern::event:"<<sce->m_requestCode<<sce->m_resultCode;
  29. if(sce->m_requestCode == 3){
  30. if(sce->m_resultCode == -1){
  31. QStringList paths = sce->m_msg.toStringList();
  32. DEBUG<<paths.count()<<paths;
  33. if(paths.isEmpty()) return QObject::event(e);
  34. setCurrentFiles(paths);
  35. setCurrentFile(paths.at(0));
  36. // QMessageBox::information(this,"warn","hello",QMessageBox::YesAll);
  37. emit accepted();
  38. }else if (sce->m_resultCode == 0) {
  39. DEBUG<<"cancel";
  40. emit rejected();
  41. }
  42. }/*else
  43. {
  44. m_captureState->setText("cancel");
  45. }*/
  46. return true;
  47. }
  48. #endif
  49. return QObject::event(e);
  50. }
  51. bool FileDialogExtern::open(){
  52. DEBUG;
  53. #ifdef ANDROID
  54. //QAndroidJniObject javaAction = QAndroidJniObject::fromString(name);
  55. g_listener = this;
  56. QAndroidJniObject::callStaticMethod<void>("an/qt/extendsQtWithJava/ExtendsQtWithJava",
  57. "fileManagerActivity",
  58. "(Z)V",(m_fileMode == OpenFiles));
  59. #else
  60. QString fdir = m_folder.isEmpty()?QDir::currentPath():m_folder;
  61. QStringList fpaths;
  62. if(m_fileMode == OpenFile){
  63. QString fpath = QFileDialog::getOpenFileName(nullptr,m_title,fdir,"all file(*)");
  64. if(!fpath.isEmpty())
  65. fpaths.push_back(fpath);
  66. }else if (m_fileMode == OpenFiles) {
  67. fpaths = QFileDialog::getOpenFileNames(nullptr,m_title,fdir,"all file(*)");
  68. }
  69. DEBUG<<fpaths.count()<<fpaths;
  70. if(!fpaths.isEmpty()){
  71. setCurrentFiles(fpaths);
  72. setCurrentFile(fpaths.at(0));
  73. emit accepted();
  74. }else {
  75. emit rejected();
  76. }
  77. #endif
  78. return true;
  79. }
  80. #ifdef ANDROID
  81. void onFileManager(JNIEnv *env, jobject thiz,int result, jobjectArray fileUrls)
  82. {
  83. qDebug() << "onFileManager, result - " << result ;
  84. int length = env->GetArrayLength(fileUrls);
  85. QStringList paths;
  86. int req = 3;
  87. int ret = result;
  88. if(result == -1){
  89. //ret = 3;
  90. for (int i=0;i<length;i++) {
  91. //jstring fileUrl= dynamic_cast<jstring>(env->GetObjectArrayElement(fileUrls,i));
  92. QString path=QAndroidJniObject::fromLocalRef(env->GetObjectArrayElement(fileUrls,i)).toString();
  93. // qDebug() << "onFileManager, result - " << result << " fileUrl - " << fileUrl;
  94. //jboolean copy = false;
  95. //const char *nativeString = env->GetStringUTFChars(fileUrl, &copy);
  96. //qDebug() << "onFileManager, nativeString - " << nativeString;
  97. // QString path = nativeString;
  98. // env->ReleaseStringUTFChars(fileUrl, nativeString);
  99. if(!path.isEmpty() && QFile::exists(path))
  100. paths.push_back(path);
  101. qDebug() << "onFileManager, path - " << path;
  102. }
  103. }else if(result != 0){
  104. ret = -3;
  105. qDebug() << "could not read the captured file!";
  106. }
  107. DEBUG<<paths;
  108. //QGuiApplication::postEvent(g_listener, new SimpleCustomEvent(ret, image));
  109. QCoreApplication::postEvent(g_listener, new SimpleCustomEvent(req,ret, paths));
  110. }
  111. #endif