window-basic-main-dropfiles.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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. QString name;
  77. obs_video_info ovi;
  78. obs_get_video_info(&ovi);
  79. switch (image) {
  80. case DropType_RawText:
  81. obs_data_set_string(settings, "text", data);
  82. #ifdef _WIN32
  83. type = "text_gdiplus";
  84. #else
  85. type = "text_ft2_source";
  86. #endif
  87. break;
  88. case DropType_Text:
  89. #ifdef _WIN32
  90. obs_data_set_bool(settings, "read_from_file", true);
  91. obs_data_set_string(settings, "file", data);
  92. name = QUrl::fromLocalFile(QString(data)).fileName();
  93. type = "text_gdiplus";
  94. #else
  95. obs_data_set_bool(settings, "from_file", true);
  96. obs_data_set_string(settings, "text_file", data);
  97. type = "text_ft2_source";
  98. #endif
  99. break;
  100. case DropType_Image:
  101. obs_data_set_string(settings, "file", data);
  102. name = QUrl::fromLocalFile(QString(data)).fileName();
  103. type = "image_source";
  104. break;
  105. case DropType_Media:
  106. obs_data_set_string(settings, "local_file", data);
  107. name = QUrl::fromLocalFile(QString(data)).fileName();
  108. type = "ffmpeg_source";
  109. break;
  110. case DropType_Html:
  111. obs_data_set_bool(settings, "is_local_file", true);
  112. obs_data_set_string(settings, "local_file", data);
  113. obs_data_set_int(settings, "width", ovi.base_width);
  114. obs_data_set_int(settings, "height", ovi.base_height);
  115. name = QUrl::fromLocalFile(QString(data)).fileName();
  116. type = "browser_source";
  117. break;
  118. case DropType_Url:
  119. AddDropURL(data, name, settings, ovi);
  120. type = "browser_source";
  121. break;
  122. }
  123. if (!obs_source_get_display_name(type)) {
  124. obs_data_release(settings);
  125. return;
  126. }
  127. if (name.isEmpty())
  128. name = obs_source_get_display_name(type);
  129. source = obs_source_create(type,
  130. GenerateSourceName(QT_TO_UTF8(name)).c_str(),
  131. settings, nullptr);
  132. if (source) {
  133. OBSScene scene = main->GetCurrentScene();
  134. obs_scene_add(scene, source);
  135. obs_source_release(source);
  136. }
  137. obs_data_release(settings);
  138. }
  139. void OBSBasic::dragEnterEvent(QDragEnterEvent *event)
  140. {
  141. event->acceptProposedAction();
  142. }
  143. void OBSBasic::dragLeaveEvent(QDragLeaveEvent *event)
  144. {
  145. event->accept();
  146. }
  147. void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
  148. {
  149. event->acceptProposedAction();
  150. }
  151. void OBSBasic::ConfirmDropUrl(const QString &url)
  152. {
  153. if (url.left(7).compare("http://", Qt::CaseInsensitive) == 0 ||
  154. url.left(8).compare("https://", Qt::CaseInsensitive) == 0) {
  155. activateWindow();
  156. QString msg = QTStr("AddUrl.Text");
  157. msg += "\n\n";
  158. msg += QTStr("AddUrl.Text.Url").arg(url);
  159. QMessageBox messageBox(this);
  160. messageBox.setWindowTitle(QTStr("AddUrl.Title"));
  161. messageBox.setText(msg);
  162. QPushButton *yesButton = messageBox.addButton(
  163. QTStr("Yes"), QMessageBox::YesRole);
  164. QPushButton *noButton =
  165. messageBox.addButton(QTStr("No"), QMessageBox::NoRole);
  166. messageBox.setDefaultButton(yesButton);
  167. messageBox.setEscapeButton(noButton);
  168. messageBox.setIcon(QMessageBox::Question);
  169. messageBox.exec();
  170. if (messageBox.clickedButton() == yesButton)
  171. AddDropSource(QT_TO_UTF8(url), DropType_Url);
  172. }
  173. }
  174. void OBSBasic::dropEvent(QDropEvent *event)
  175. {
  176. const QMimeData *mimeData = event->mimeData();
  177. if (mimeData->hasUrls()) {
  178. QList<QUrl> urls = mimeData->urls();
  179. for (int i = 0; i < urls.size(); i++) {
  180. QUrl url = urls[i];
  181. QString file = url.toLocalFile();
  182. QFileInfo fileInfo(file);
  183. if (!fileInfo.exists()) {
  184. ConfirmDropUrl(url.url());
  185. continue;
  186. }
  187. QString suffixQStr = fileInfo.suffix();
  188. QByteArray suffixArray = suffixQStr.toUtf8();
  189. const char *suffix = suffixArray.constData();
  190. bool found = false;
  191. const char **cmp;
  192. #define CHECK_SUFFIX(extensions, type) \
  193. cmp = extensions; \
  194. while (*cmp) { \
  195. if (strcmp(*cmp, suffix) == 0) { \
  196. AddDropSource(QT_TO_UTF8(file), type); \
  197. found = true; \
  198. break; \
  199. } \
  200. \
  201. cmp++; \
  202. } \
  203. \
  204. if (found) \
  205. continue;
  206. CHECK_SUFFIX(textExtensions, DropType_Text);
  207. CHECK_SUFFIX(htmlExtensions, DropType_Html);
  208. CHECK_SUFFIX(imageExtensions, DropType_Image);
  209. CHECK_SUFFIX(mediaExtensions, DropType_Media);
  210. #undef CHECK_SUFFIX
  211. }
  212. } else if (mimeData->hasText()) {
  213. AddDropSource(QT_TO_UTF8(mimeData->text()), DropType_RawText);
  214. }
  215. }