window-basic-main-dropfiles.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. #include <QDragEnterEvent>
  2. #include <QDragLeaveEvent>
  3. #include <QDragMoveEvent>
  4. #include <QDropEvent>
  5. #include <QFileInfo>
  6. #include <QMimeData>
  7. #include <QUrlQuery>
  8. #include <string>
  9. #include "window-basic-main.hpp"
  10. #include "qt-wrappers.hpp"
  11. using namespace std;
  12. static const char *textExtensions[] = {"txt", "log", nullptr};
  13. static const char *imageExtensions[] = {"bmp", "tga", "png", "jpg",
  14. "jpeg", "gif", nullptr};
  15. static const char *htmlExtensions[] = {"htm", "html", nullptr};
  16. static const char *mediaExtensions[] = {
  17. "3ga", "669", "a52", "aac", "ac3", "adt", "adts", "aif",
  18. "aifc", "aiff", "amb", "amr", "aob", "ape", "au", "awb",
  19. "caf", "dts", "flac", "it", "kar", "m4a", "m4b", "m4p",
  20. "m5p", "mid", "mka", "mlp", "mod", "mpa", "mp1", "mp2",
  21. "mp3", "mpc", "mpga", "mus", "oga", "ogg", "oma", "opus",
  22. "qcp", "ra", "rmi", "s3m", "sid", "spx", "tak", "thd",
  23. "tta", "voc", "vqf", "w64", "wav", "wma", "wv", "xa",
  24. "xm", "3g2", "3gp", "3gp2", "3gpp", "amv", "asf", "avi",
  25. "bik", "crf", "divx", "drc", "dv", "evo", "f4v", "flv",
  26. "gvi", "gxf", "iso", "m1v", "m2v", "m2t", "m2ts", "m4v",
  27. "mkv", "mov", "mp2", "mp2v", "mp4", "mp4v", "mpe", "mpeg",
  28. "mpeg1", "mpeg2", "mpeg4", "mpg", "mpv2", "mts", "mtv", "mxf",
  29. "mxg", "nsv", "nuv", "ogg", "ogm", "ogv", "ogx", "ps",
  30. "rec", "rm", "rmvb", "rpl", "thp", "tod", "ts", "tts",
  31. "txd", "vob", "vro", "webm", "wm", "wmv", "wtv", nullptr};
  32. static string GenerateSourceName(const char *base)
  33. {
  34. string name;
  35. int inc = 0;
  36. for (;; inc++) {
  37. name = base;
  38. if (inc) {
  39. name += " (";
  40. name += to_string(inc + 1);
  41. name += ")";
  42. }
  43. obs_source_t *source = obs_get_source_by_name(name.c_str());
  44. if (!source)
  45. return name;
  46. }
  47. }
  48. void OBSBasic::AddDropURL(const char *url, QString &name, obs_data_t *settings,
  49. const obs_video_info &ovi)
  50. {
  51. QUrl path = QString::fromUtf8(url);
  52. QUrlQuery query = QUrlQuery(path.query(QUrl::FullyEncoded));
  53. int cx = (int)ovi.base_width;
  54. int cy = (int)ovi.base_height;
  55. if (query.hasQueryItem("layer-width"))
  56. cx = query.queryItemValue("layer-width").toInt();
  57. if (query.hasQueryItem("layer-height"))
  58. cy = query.queryItemValue("layer-height").toInt();
  59. obs_data_set_int(settings, "width", cx);
  60. obs_data_set_int(settings, "height", cy);
  61. name = query.hasQueryItem("layer-name")
  62. ? query.queryItemValue("layer-name", QUrl::FullyDecoded)
  63. : path.host();
  64. query.removeQueryItem("layer-width");
  65. query.removeQueryItem("layer-height");
  66. query.removeQueryItem("layer-name");
  67. path.setQuery(query);
  68. obs_data_set_string(settings, "url", QT_TO_UTF8(path.url()));
  69. }
  70. void OBSBasic::AddDropSource(const char *data, DropType image)
  71. {
  72. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  73. obs_data_t *settings = obs_data_create();
  74. obs_source_t *source = nullptr;
  75. const char *type = nullptr;
  76. std::vector<const char *> types;
  77. QString name;
  78. obs_video_info ovi;
  79. obs_get_video_info(&ovi);
  80. switch (image) {
  81. case DropType_RawText:
  82. obs_data_set_string(settings, "text", data);
  83. #ifdef _WIN32
  84. type = "text_gdiplus";
  85. #else
  86. type = "text_ft2_source";
  87. #endif
  88. break;
  89. case DropType_Text:
  90. #ifdef _WIN32
  91. obs_data_set_bool(settings, "read_from_file", true);
  92. obs_data_set_string(settings, "file", data);
  93. name = QUrl::fromLocalFile(QString(data)).fileName();
  94. type = "text_gdiplus";
  95. #else
  96. obs_data_set_bool(settings, "from_file", true);
  97. obs_data_set_string(settings, "text_file", data);
  98. type = "text_ft2_source";
  99. #endif
  100. break;
  101. case DropType_Image:
  102. obs_data_set_string(settings, "file", data);
  103. name = QUrl::fromLocalFile(QString(data)).fileName();
  104. type = "image_source";
  105. break;
  106. case DropType_Media:
  107. obs_data_set_string(settings, "local_file", data);
  108. name = QUrl::fromLocalFile(QString(data)).fileName();
  109. type = "ffmpeg_source";
  110. break;
  111. case DropType_Html:
  112. obs_data_set_bool(settings, "is_local_file", true);
  113. obs_data_set_string(settings, "local_file", data);
  114. obs_data_set_int(settings, "width", ovi.base_width);
  115. obs_data_set_int(settings, "height", ovi.base_height);
  116. name = QUrl::fromLocalFile(QString(data)).fileName();
  117. types = {"browser_source", "linuxbrowser-source"};
  118. break;
  119. case DropType_Url:
  120. AddDropURL(data, name, settings, ovi);
  121. types = {"browser_source", "linuxbrowser-source"};
  122. break;
  123. }
  124. for (char const *t : types) {
  125. if (obs_source_get_display_name(t)) {
  126. type = t;
  127. break;
  128. }
  129. }
  130. if (type == nullptr || !obs_source_get_display_name(type)) {
  131. obs_data_release(settings);
  132. return;
  133. }
  134. if (name.isEmpty())
  135. name = obs_source_get_display_name(type);
  136. source = obs_source_create(type,
  137. GenerateSourceName(QT_TO_UTF8(name)).c_str(),
  138. settings, nullptr);
  139. if (source) {
  140. OBSScene scene = main->GetCurrentScene();
  141. obs_scene_add(scene, source);
  142. obs_source_release(source);
  143. }
  144. obs_data_release(settings);
  145. }
  146. void OBSBasic::dragEnterEvent(QDragEnterEvent *event)
  147. {
  148. event->acceptProposedAction();
  149. }
  150. void OBSBasic::dragLeaveEvent(QDragLeaveEvent *event)
  151. {
  152. event->accept();
  153. }
  154. void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
  155. {
  156. event->acceptProposedAction();
  157. }
  158. void OBSBasic::ConfirmDropUrl(const QString &url)
  159. {
  160. if (url.left(7).compare("http://", Qt::CaseInsensitive) == 0 ||
  161. url.left(8).compare("https://", Qt::CaseInsensitive) == 0) {
  162. activateWindow();
  163. QString msg = QTStr("AddUrl.Text");
  164. msg += "\n\n";
  165. msg += QTStr("AddUrl.Text.Url").arg(url);
  166. QMessageBox messageBox(this);
  167. messageBox.setWindowTitle(QTStr("AddUrl.Title"));
  168. messageBox.setText(msg);
  169. QPushButton *yesButton = messageBox.addButton(
  170. QTStr("Yes"), QMessageBox::YesRole);
  171. QPushButton *noButton =
  172. messageBox.addButton(QTStr("No"), QMessageBox::NoRole);
  173. messageBox.setDefaultButton(yesButton);
  174. messageBox.setEscapeButton(noButton);
  175. messageBox.setIcon(QMessageBox::Question);
  176. messageBox.exec();
  177. if (messageBox.clickedButton() == yesButton)
  178. AddDropSource(QT_TO_UTF8(url), DropType_Url);
  179. }
  180. }
  181. void OBSBasic::dropEvent(QDropEvent *event)
  182. {
  183. const QMimeData *mimeData = event->mimeData();
  184. if (mimeData->hasUrls()) {
  185. QList<QUrl> urls = mimeData->urls();
  186. for (int i = 0; i < urls.size(); i++) {
  187. QUrl url = urls[i];
  188. QString file = url.toLocalFile();
  189. QFileInfo fileInfo(file);
  190. if (!fileInfo.exists()) {
  191. ConfirmDropUrl(url.url());
  192. continue;
  193. }
  194. QString suffixQStr = fileInfo.suffix();
  195. QByteArray suffixArray = suffixQStr.toUtf8();
  196. const char *suffix = suffixArray.constData();
  197. bool found = false;
  198. const char **cmp;
  199. #define CHECK_SUFFIX(extensions, type) \
  200. cmp = extensions; \
  201. while (*cmp) { \
  202. if (strcmp(*cmp, suffix) == 0) { \
  203. AddDropSource(QT_TO_UTF8(file), type); \
  204. found = true; \
  205. break; \
  206. } \
  207. \
  208. cmp++; \
  209. } \
  210. \
  211. if (found) \
  212. continue;
  213. CHECK_SUFFIX(textExtensions, DropType_Text);
  214. CHECK_SUFFIX(htmlExtensions, DropType_Html);
  215. CHECK_SUFFIX(imageExtensions, DropType_Image);
  216. CHECK_SUFFIX(mediaExtensions, DropType_Media);
  217. #undef CHECK_SUFFIX
  218. }
  219. } else if (mimeData->hasText()) {
  220. AddDropSource(QT_TO_UTF8(mimeData->text()), DropType_RawText);
  221. }
  222. }