1
0

window-basic-main-dropfiles.cpp 9.9 KB

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