window-basic-main-dropfiles.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. #include <QDragEnterEvent>
  2. #include <QDragLeaveEvent>
  3. #include <QDragMoveEvent>
  4. #include <QDropEvent>
  5. #include <QFileInfo>
  6. #include <QMimeData>
  7. #include <QUrlQuery>
  8. #ifdef _WIN32
  9. #include <QSettings>
  10. #endif
  11. #include <string>
  12. #include <qt-wrappers.hpp>
  13. #include "window-basic-main.hpp"
  14. using namespace std;
  15. static const char *textExtensions[] = {"txt", "log", nullptr};
  16. static const char *imageExtensions[] = {"bmp", "gif", "jpeg", "jpg",
  17. #ifdef _WIN32
  18. "jxr",
  19. #endif
  20. "png", "tga", "webp", nullptr};
  21. static const char *htmlExtensions[] = {"htm", "html", nullptr};
  22. static const char *mediaExtensions[] = {
  23. "3ga", "669", "a52", "aac", "ac3", "adt", "adts", "aif", "aifc", "aiff", "amb", "amr", "aob", "ape",
  24. "au", "awb", "caf", "dts", "flac", "it", "kar", "m4a", "m4b", "m4p", "m5p", "mid", "mka", "mlp",
  25. "mod", "mpa", "mp1", "mp2", "mp3", "mpc", "mpga", "mus", "oga", "ogg", "oma", "opus", "qcp", "ra",
  26. "rmi", "s3m", "sid", "spx", "tak", "thd", "tta", "voc", "vqf", "w64", "wav", "wma", "wv", "xa",
  27. "xm", "3g2", "3gp", "3gp2", "3gpp", "amv", "asf", "avi", "bik", "crf", "divx", "drc", "dv", "evo",
  28. "f4v", "flv", "gvi", "gxf", "iso", "m1v", "m2v", "m2t", "m2ts", "m4v", "mkv", "mov", "mp2", "mp2v",
  29. "mp4", "mp4v", "mpe", "mpeg", "mpeg1", "mpeg2", "mpeg4", "mpg", "mpv2", "mts", "mtv", "mxf", "mxg", "nsv",
  30. "nuv", "ogg", "ogm", "ogv", "ogx", "ps", "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. OBSSourceAutoRelease source = obs_get_source_by_name(name.c_str());
  44. if (!source)
  45. return name;
  46. }
  47. }
  48. #ifdef _WIN32
  49. static QString ReadWindowsURLFile(const QString &file)
  50. {
  51. QSettings iniFile(file, QSettings::IniFormat);
  52. QVariant url = iniFile.value("InternetShortcut/URL");
  53. return url.toString();
  54. }
  55. #endif
  56. void OBSBasic::AddDropURL(const char *url, QString &name, obs_data_t *settings, const obs_video_info &ovi)
  57. {
  58. QUrl path = QString::fromUtf8(url);
  59. QUrlQuery query = QUrlQuery(path.query(QUrl::FullyEncoded));
  60. int cx = (int)ovi.base_width;
  61. int cy = (int)ovi.base_height;
  62. if (query.hasQueryItem("layer-width"))
  63. cx = query.queryItemValue("layer-width").toInt();
  64. if (query.hasQueryItem("layer-height"))
  65. cy = query.queryItemValue("layer-height").toInt();
  66. if (query.hasQueryItem("layer-css")) {
  67. // QUrl::FullyDecoded does NOT properly decode a
  68. // application/x-www-form-urlencoded space represented as '+'
  69. // Thus, this is manually filtered out before QUrl's
  70. // decoding kicks in again. This is to allow JavaScript's
  71. // default searchParams.append function to simply append css
  72. // to the query parameters, which is the intended usecase for this.
  73. QString fullyEncoded = query.queryItemValue("layer-css", QUrl::FullyEncoded);
  74. fullyEncoded = fullyEncoded.replace("+", "%20");
  75. QString decoded = QUrl::fromPercentEncoding(QByteArray::fromStdString(QT_TO_UTF8(fullyEncoded)));
  76. obs_data_set_string(settings, "css", QT_TO_UTF8(decoded));
  77. }
  78. obs_data_set_int(settings, "width", cx);
  79. obs_data_set_int(settings, "height", cy);
  80. name = query.hasQueryItem("layer-name") ? query.queryItemValue("layer-name", QUrl::FullyDecoded) : path.host();
  81. query.removeQueryItem("layer-width");
  82. query.removeQueryItem("layer-height");
  83. query.removeQueryItem("layer-name");
  84. query.removeQueryItem("layer-css");
  85. path.setQuery(query);
  86. obs_data_set_string(settings, "url", QT_TO_UTF8(path.url()));
  87. }
  88. void OBSBasic::AddDropSource(const char *data, DropType image)
  89. {
  90. OBSBasic *main = reinterpret_cast<OBSBasic *>(App()->GetMainWindow());
  91. OBSDataAutoRelease settings = obs_data_create();
  92. const char *type = nullptr;
  93. QString name;
  94. obs_video_info ovi;
  95. obs_get_video_info(&ovi);
  96. switch (image) {
  97. case DropType_RawText:
  98. obs_data_set_string(settings, "text", data);
  99. #ifdef _WIN32
  100. type = "text_gdiplus";
  101. #else
  102. type = "text_ft2_source";
  103. #endif
  104. break;
  105. case DropType_Text:
  106. #ifdef _WIN32
  107. obs_data_set_bool(settings, "read_from_file", true);
  108. obs_data_set_string(settings, "file", data);
  109. name = QUrl::fromLocalFile(QString(data)).fileName();
  110. type = "text_gdiplus";
  111. #else
  112. obs_data_set_bool(settings, "from_file", true);
  113. obs_data_set_string(settings, "text_file", data);
  114. type = "text_ft2_source";
  115. #endif
  116. break;
  117. case DropType_Image:
  118. obs_data_set_string(settings, "file", data);
  119. name = QUrl::fromLocalFile(QString(data)).fileName();
  120. type = "image_source";
  121. break;
  122. case DropType_Media:
  123. obs_data_set_string(settings, "local_file", data);
  124. name = QUrl::fromLocalFile(QString(data)).fileName();
  125. type = "ffmpeg_source";
  126. break;
  127. case DropType_Html:
  128. obs_data_set_bool(settings, "is_local_file", true);
  129. obs_data_set_string(settings, "local_file", data);
  130. obs_data_set_int(settings, "width", ovi.base_width);
  131. obs_data_set_int(settings, "height", ovi.base_height);
  132. name = QUrl::fromLocalFile(QString(data)).fileName();
  133. type = "browser_source";
  134. break;
  135. case DropType_Url:
  136. AddDropURL(data, name, settings, ovi);
  137. type = "browser_source";
  138. break;
  139. }
  140. type = obs_get_latest_input_type_id(type);
  141. if (type == nullptr || !obs_source_get_display_name(type)) {
  142. return;
  143. }
  144. if (name.isEmpty())
  145. name = obs_source_get_display_name(type);
  146. std::string sourceName = GenerateSourceName(QT_TO_UTF8(name));
  147. OBSSourceAutoRelease source = obs_source_create(type, sourceName.c_str(), settings, nullptr);
  148. if (source) {
  149. OBSDataAutoRelease wrapper = obs_save_source(source);
  150. OBSScene scene = main->GetCurrentScene();
  151. std::string sceneUUID = obs_source_get_uuid(obs_scene_get_source(scene));
  152. std::string sourceUUID = obs_source_get_uuid(source);
  153. auto undo = [sceneUUID, sourceUUID](const std::string &) {
  154. OBSSourceAutoRelease source = obs_get_source_by_uuid(sourceUUID.c_str());
  155. obs_source_remove(source);
  156. OBSSourceAutoRelease scene = obs_get_source_by_uuid(sceneUUID.c_str());
  157. OBSBasic::Get()->SetCurrentScene(scene.Get(), true);
  158. };
  159. auto redo = [sceneUUID, sourceName, type](const std::string &data) {
  160. OBSSourceAutoRelease scene = obs_get_source_by_uuid(sceneUUID.c_str());
  161. OBSBasic::Get()->SetCurrentScene(scene.Get(), true);
  162. OBSDataAutoRelease dat = obs_data_create_from_json(data.c_str());
  163. OBSSourceAutoRelease source = obs_load_source(dat);
  164. obs_scene_add(obs_scene_from_source(scene), source.Get());
  165. };
  166. undo_s.add_action(QTStr("Undo.Add").arg(sourceName.c_str()), undo, redo, "",
  167. std::string(obs_data_get_json(wrapper)));
  168. obs_scene_add(scene, source);
  169. }
  170. }
  171. void OBSBasic::dragEnterEvent(QDragEnterEvent *event)
  172. {
  173. // refuse drops of our own widgets
  174. if (event->source() != nullptr) {
  175. event->setDropAction(Qt::IgnoreAction);
  176. return;
  177. }
  178. event->acceptProposedAction();
  179. }
  180. void OBSBasic::dragLeaveEvent(QDragLeaveEvent *event)
  181. {
  182. event->accept();
  183. }
  184. void OBSBasic::dragMoveEvent(QDragMoveEvent *event)
  185. {
  186. event->acceptProposedAction();
  187. }
  188. void OBSBasic::ConfirmDropUrl(const QString &url)
  189. {
  190. if (url.left(7).compare("http://", Qt::CaseInsensitive) == 0 ||
  191. url.left(8).compare("https://", Qt::CaseInsensitive) == 0) {
  192. activateWindow();
  193. QString msg = QTStr("AddUrl.Text");
  194. msg += "\n\n";
  195. msg += QTStr("AddUrl.Text.Url").arg(url);
  196. QMessageBox messageBox(this);
  197. messageBox.setWindowTitle(QTStr("AddUrl.Title"));
  198. messageBox.setText(msg);
  199. QPushButton *yesButton = messageBox.addButton(QTStr("Yes"), QMessageBox::YesRole);
  200. QPushButton *noButton = messageBox.addButton(QTStr("No"), QMessageBox::NoRole);
  201. messageBox.setDefaultButton(yesButton);
  202. messageBox.setEscapeButton(noButton);
  203. messageBox.setIcon(QMessageBox::Question);
  204. messageBox.exec();
  205. if (messageBox.clickedButton() == yesButton)
  206. AddDropSource(QT_TO_UTF8(url), DropType_Url);
  207. }
  208. }
  209. void OBSBasic::dropEvent(QDropEvent *event)
  210. {
  211. const QMimeData *mimeData = event->mimeData();
  212. if (mimeData->hasUrls()) {
  213. QList<QUrl> urls = mimeData->urls();
  214. for (int i = 0; i < urls.size(); i++) {
  215. QUrl url = urls[i];
  216. QString file = url.toLocalFile();
  217. QFileInfo fileInfo(file);
  218. if (!fileInfo.exists()) {
  219. ConfirmDropUrl(url.url());
  220. continue;
  221. }
  222. #ifdef _WIN32
  223. if (fileInfo.suffix().compare("url", Qt::CaseInsensitive) == 0) {
  224. QString urlTarget = ReadWindowsURLFile(file);
  225. if (!urlTarget.isEmpty()) {
  226. ConfirmDropUrl(urlTarget);
  227. }
  228. continue;
  229. } else if (fileInfo.isShortcut()) {
  230. file = fileInfo.symLinkTarget();
  231. fileInfo = QFileInfo(file);
  232. if (!fileInfo.exists()) {
  233. continue;
  234. }
  235. }
  236. #endif
  237. QString suffixQStr = fileInfo.suffix();
  238. QByteArray suffixArray = suffixQStr.toUtf8();
  239. const char *suffix = suffixArray.constData();
  240. bool found = false;
  241. const char **cmp;
  242. #define CHECK_SUFFIX(extensions, type) \
  243. cmp = extensions; \
  244. while (*cmp) { \
  245. if (astrcmpi(*cmp, suffix) == 0) { \
  246. AddDropSource(QT_TO_UTF8(file), type); \
  247. found = true; \
  248. break; \
  249. } \
  250. \
  251. cmp++; \
  252. } \
  253. \
  254. if (found) \
  255. continue;
  256. CHECK_SUFFIX(textExtensions, DropType_Text);
  257. CHECK_SUFFIX(htmlExtensions, DropType_Html);
  258. CHECK_SUFFIX(imageExtensions, DropType_Image);
  259. CHECK_SUFFIX(mediaExtensions, DropType_Media);
  260. #undef CHECK_SUFFIX
  261. }
  262. } else if (mimeData->hasText()) {
  263. AddDropSource(QT_TO_UTF8(mimeData->text()), DropType_RawText);
  264. }
  265. }